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

本文介绍如何在C++环境中安装和配置MySQL Connector,包括安装Boost库、下载MySQL Connector头文件及库文件,并提供了示例代码及编译命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://blog.youkuaiyun.com/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):
#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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值