mysql connector c++ 1.1 安装

mysql connector c++ 1.1 安装

前提:boost安装完毕

注意:8.0版本的源码用此方法,编译不过。所以要编译8.0的同学请绕行。

1,下载源码

git clone https://github.com/mysql/mysql-connector-cpp.git

2,进入下载下来的文件夹

cd mysql-connector-cpp

3,由于下载下来不是1.1版本,切换成1.1版本

git checkout 1.1

4,执行cmake,指定boost的安装路径

cmake -DBOOST_ROOT=/home/ys/boost_1_70_0

5,执行make

make

6,执行make install

make install

如果编译成功会有下面的结果:

把编译生成的动态库文件和静态库文件放到了/usr/local/lib下了;把头文件放到/usr/local/include下了。

Install the project...
-- Install configuration: ""
-- Installing: /usr/local/./README
-- Installing: /usr/local/./LICENSE
-- Installing: /usr/local/./BUILDINFO
-- Installing: /usr/local/include/cppconn/build_config.h
-- Installing: /usr/local/include/cppconn/config.h
-- Installing: /usr/local/include/cppconn/connection.h
-- Installing: /usr/local/include/cppconn/datatype.h
-- Installing: /usr/local/include/cppconn/driver.h
-- Installing: /usr/local/include/cppconn/exception.h
-- Installing: /usr/local/include/cppconn/metadata.h
-- Installing: /usr/local/include/cppconn/parameter_metadata.h
-- Installing: /usr/local/include/cppconn/prepared_statement.h
-- Installing: /usr/local/include/cppconn/resultset.h
-- Installing: /usr/local/include/cppconn/resultset_metadata.h
-- Installing: /usr/local/include/cppconn/statement.h
-- Installing: /usr/local/include/cppconn/sqlstring.h
-- Installing: /usr/local/include/cppconn/warning.h
-- Installing: /usr/local/include/cppconn/version_info.h
-- Installing: /usr/local/include/cppconn/variant.h
-- Installing: /usr/local/lib/libmysqlcppconn.so.7.1.1.12
-- Installing: /usr/local/lib/libmysqlcppconn.so.7
-- Installing: /usr/local/lib/libmysqlcppconn.so
-- Installing: /usr/local/lib/libmysqlcppconn-static.a
-- Installing: /usr/local/include/mysql_connection.h
-- Installing: /usr/local/include/mysql_driver.h
-- Installing: /usr/local/include/mysql_error.h

API使用的官方文档:

https://dev.mysql.com/doc/connector-cpp/1.1/en/preface.html

测试程序:

#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/driver.h>

using namespace sql;
using namespace std;

#define DBHOST "tcp://127.0.0.1:3306"
#define USER "userName"
#define PASSWORD "your password"

int main(){

  Driver *driver;
  Connection *conn;
  driver = get_driver_instance();
  conn = driver->connect(DBHOST, USER, PASSWORD);
  cout<<"DataBase connection autocommit mode = "<<conn->getAutoCommit()<<endl;
  delete conn;
  driver = NULL;
  conn = NULL;
  return 0;
}

编译方法:注意加上动态库mysqlcppconn

g++ test.cpp -lmysqlcppconn

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

原文地址:https://www.cnblogs.com/xiaoshiwang/p/11544095.html