cpp_对接mysql数据库方案

最近计划把python的web项目迁移到cpp上。需要对接mysql,sqlite数据库。
调研了下目前cpp对接mysql大约三种解决方案。
源代码和相关脚本参考github:

c查询mysql方案和demo(mysql.h)

使用c语言和mysql的对接demo
依赖安装包:

1
2
sudo apt install mysql-server mysql-client
sudo apt install libmysqlclient-dev

验证安装ok:

1
2
3
ubuntu@VM-16-5-ubuntu:~/cpp_db_conn$ mysql_config --cflags --libs
-I/usr/include/mysql
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -lrt -lssl -lcrypto -ldl

创建数据库和测试表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE DATABASE IF NOT EXISTS test
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;

use test;

DROP TABLE IF EXISTS `dept`;

CREATE TABLE `dept` (
`deptno` tinyint(2) NOT NULL,
`dname` varchar(14) DEFAULT NULL,
`loc` varchar(13) DEFAULT NULL,
PRIMARY KEY (`deptno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `dept` VALUES (10,'部门ACCOUNTING','NEW YORK'),(20,'部门RESEARCH','DALLAS'),(30,'部门SALES','CHICAGO'),(40,'部门OPERATIONS','BOSTON ');

编译和运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mkdir build
cd build
cmake ..
make

ubuntu@VM-16-5-ubuntu:~/cpp_db_conn/build$ ./c_mysql_conn
connect success!
insert OK
查询到 6 行
deptno dname loc
10 部门ACCOUNTING NEW YORK
20 部门RESEARCH DALLAS
30 部门SALES CHICAGO
40 部门OPERATIONS BOSTON
111 research new york
delete OK

cpp查询mysql方案(cpp-mysql-conn)

依赖安装包:

1
2
3
4
5
sudo apt-get install mysql-server mysql-client
sudo apt-get install libboost-dev
sudo apt-get install libcpprest-dev
sudo apt-get install libmysqlclient-dev -y
sudo apt-get install libmysqlcppconn-dev

编译命令:
gcc编译

1
2
cpp_mysql_conn.cpp  
g++ cpp_mysql_conn.cpp -o test -lmysqlcppconn

cmake编译和运行

1
2
3
4
5
6
7
8
9
mkdir build
cd build
cmake ..
make

ubuntu@VM-16-5-ubuntu:~/cpp_db_conn/build$ ./cpp_mysql_conn
Running 'SELECT 'Hello World!' AS _message'...
... MySQL replies: Hello World!
... MySQL says it again: Hello World!

cpp查询sqlite方案cpp-sqlite-conn

依赖安装包:

1
sudo apt-get install libsqlite3-dev

编译报错:

1
2
error while loading shared librariesLlibodo-sqlite-2.4.0.so:can't open shared object file:no such file or directory
ldd driver

libodo-sqlite-2.4.0.so的确对应空,但是find /usr/ -name libodo-sqlite-2.4.0.so是存在的。说明链接文件新安装未来得及刷新?
sudo ldconfig
再ldd driver有了。

执行同之前的(需要自己创建sqlitedb文件)

cpp查询mysql的orm方案(odb)

关于cpp的orm框架比对

选择odb是因为用的最多,而且据说可用于生产环境。

下载相关源码

1
2
3
4
5
6
wget https://www.codesynthesis.com/download/odb/2.4/libodb-2.4.0.tar.gz
wget https://www.codesynthesis.com/download/odb/2.4/odb-2.4.0.tar.gz
wget https://www.codesynthesis.com/download/odb/2.4/libodb-mysql-2.4.0.tar.gz
wget https://www.codesynthesis.com/download/odb/2.4/libodb-sqlite-2.4.0.tar.gz
wget https://www.codesynthesis.com/download/odb/2.4/libodb-boost-2.4.0.tar.gz
wget https://www.codesynthesis.com/download/odb/2.4/odb-examples-2.4.0.tar.gz

ODB Compliler,odb的执行文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
解压:odb-2.4.0.tar.gz
./configure
need
01,报错configure: error: GCC plugin headers not found; consider installing GCC plugin development package
解决:sudo apt-get install gcc-5-plugin-dev
02,报错configure: error: libcutl is not found; consider using --with-libcutl=DIR
解决:libcutl:wget https://www.codesynthesis.com/download/libcutl/1.10/libcutl-1.10.0.tar.gz
安装libcutl(./confgigure,make,make install)
make
make install
验证:odb --version
odb: error while loading shared libraries: libcutl-1.10.so: cannot open shared object file: No such file or directory
sudo ln -s /usr/local/lib/libcutl-1.10.so /usr/lib/libcutl-1.10.so
验证:odb --version,ok

Common Runtime Library,libodb-2.4.0,odb的核心库

1
2
解压libodb-2.4.0.tar.gz
./configure && make && make install

Database Runtime Libraries,libodb-mysql-2.4.0,与mysql交互时要用到的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
解压libodb-mysql-2.4.0
./configure
configure: error: libmysqlclient_r is not found; consider using CPPFLAGS/LDFLAGS to specify its location
sudo apt-get install libmysqlclient-dev -y
sudo ln -s /usr/lib/x86_64-linux-gnu/libmysqlclient.so /usr/lib/libmysqlclient_r.so
make
make install

解压libodb-sqlite-2.4.0.tar.gz
./configure
configure: error: libsqlite3 is not found; consider using CPPFLAGS/LDFLAGS to specify its location
sudo apt-get install libsqlite3-dev
make
make install

Profile Libraries

1
2
3
4
5
6
7
解压libodb-boost-2.4.0
./configure
configure: error: boost is not found; consider using CPPFLAGS/LDFLAGS or --with-boost=DIR to specify its location
sudo apt-get install libboost-all-dev
不确定下面还是否有必要:
make
make install

Tests and Examples,odb-examples-2.4.0,运行实例

1
2
3
4
5
6
解压odb-examples-2.4.0.tar.gz
./configure --with-database=mysql

修改mysql.options的用户名密码。修改之后也没用,不清楚读取的到底是哪个配置文件
make
make check

cpp查询mysql的orm方案_ubuntu18

差异01:ODB Compliler,odb的执行文件

1
2
3
4
解压:odb-2.4.0.tar.gz
./configure
need
01,sudo apt-get install gcc-7-plugin-dev #这里从5变成7

不过源码安装后面还会有问题(不采用官方源码,采用apt-get source xxx方式下载的源码,则可以正常编译)
最终采用apt-get install odb 方式安装。

差异02:最后一步,
Tests and Examples,odb-examples-2.4.0,运行实例

1
2
3
4
5
6
7
sudo apt-get install mysql-client  

解压odb-examples-2.4.0.tar.gz
./configure --with-database=mysql

修改mysql.options的用户名密码。修改之后也没用,不清楚读取的到底是哪个配置文件
make

ubuntu18会报错:

解决方案:

1
2
3
4
5
sudo apt-get install gcc-7-base=7.3.0-16ubuntu3 cpp-7=7.3.0-16ubuntu3 gcc-7=7.3.0-16ubuntu3 libgcc-7-dev=7.3.0-16ubuntu3 libasan4=7.3.0-16ubuntu3 libubsan0=7.3.0-16ubuntu3 libcilkrts5=7.3.0-16ubuntu3  
sudo apt-get install g++-7=7.3.0-16ubuntu3 libstdc++-7-dev=7.3.0-16ubuntu3
sudo ln -s /usr/bin/g++-7 /usr/bin/g++
sudo ln -s /usr/bin/gcc-7 /usr/bin/gcc
sudo ln -s /usr/bin/gcc /usr/bin/cc

之后就ok了

参考

ubuntu安装mysql的c++开发环境:https://blog.csdn.net/gsl371/article/details/79181856
[转](转载+整理)超详细的cmake教程:https://blog.csdn.net/u011331731/article/details/111404706#t9
Linux:Ubuntu18.04安装及配置mysql connector C++:https://blog.csdn.net/weixin_38815609/article/details/102855991
ubuntu安装mysql的c++开发环境:https://blog.csdn.net/gsl371/article/details/79181856
linux下C++连接sqlite3:https://blog.csdn.net/feinifi/article/details/87922113
ODB安装官方文档:https://www.codesynthesis.com/products/odb/doc/install-unix.xhtml
ODB安装源代码包下载:https://www.codesynthesis.com/products/odb/download.xhtml
Ubuntu 使用C++ ORM框架–ODB(odb使用实例):https://blog.csdn.net/Tulaimes/article/details/71403022
介绍一个C++的ORM工具ODB(多orm框架比较。优缺点介绍):https://blog.csdn.net/CAICHAO1234/article/details/8997051
What causes gcc invalid option for mtune?:https://stackoverflow.com/questions/60762771/what-causes-gcc-invalid-option-for-mtune

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×