您的位置:首页 > 运维架构 > Linux

Linux下安装使用mysql connector(C++)

2015-12-08 10:51 337 查看
http://blog.csdn.net/cscmaker/article/details/7468374

(1)使用C++版本的mysql connector首先需要安装和编译boost库。

   可以在boost官网上下载源文件,自己进行编译。也可以直接使用命令下载和编译,具体命令为:

   apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-*

(2)然后需要下载mysql connector的头文件和库,

           下载地址为:

           http://www.mysql.com/downloads/connector/cpp/

(3)下载以后将文件进行解压

           然后将文件夹中的include中的文件和lib中的文件分别拷到/usr/include和/usr/lib中。

(4)使用代码测试(testmysql.cpp):

        

[cpp] view
plaincopy

#include <iostream>  

#include <sstream>  

#include <memory>  

#include <string>  

#include <stdexcept>  

  

using namespace std;  

  

#include <mysql_connection.h>  

#include <mysql_driver.h>  

#include <cppconn/driver.h>  

  

using namespace sql;  

  

#define DBHOST "tcp://127.0.0.1:3306"  

#define USER "root"  

#define PASSWORD "your password here"  

  

int main() {  

   Driver *driver;  

   Connection *conn;  

   driver = get_driver_instance();  

   conn = driver->connect(DBHOST, USER, PASSWORD);  

   conn->setAutoCommit(0);  

   cout<<"DataBase connection autocommit mode = "<<conn->getAutoCommit()<<endl;  

   delete conn;  

   driver = NULL;  

   conn = NULL;  

   return 0;  

}  

然后对该文件进行编译:g++ -o testmysql -lmysqlcppconn testmysql.cpp

注意编译时不要忘了使用-lmysqlcppconn。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: