Mysql Connector/C++ 使用介紹
一、下載mysql connector c++和 boost庫
wget https://dev.mysql.com/get/Downloads/Connector-C++/mysql-connector-c++-1.1.8-linux-glibc2.5-x86-64bit.tar.gz
下載:根據自己的系統下載mysql connector c++版本,我們直接下載已經編譯好壓縮包,直接使用包裏面的inlcude文件以及a文件或者so文件就可以了。當然你也可以下source源碼文件自己編譯安裝。我是用官網的編譯好的包,如下面所示。
解壓出來目錄如下:(只要把include和lib目錄包含到自己的項目目錄就可以了)
如果自己想從source源碼編譯出來:請按照下圖頁面下載就可以:
二、下載boost庫編譯安裝boost庫,因爲mysql connector c++需要boost庫的支持:(當然你可以直接使用yum安裝)
wget https://sourceforge.net/projects/boost/files/boost/1.61.0/boost_1_61_0.tar.gz
然後解壓安裝,根據自己的實際情況選擇安裝路勁。我是默認安裝的,安裝步驟如下:
./bootstrap.sh
./b2
./b2 isntall
三.下面提供一個本人編寫連接池,根據網上一些資料修改的。
/*
time: 2017.3.31
author: chenxun [email protected]
notice: MySql Connector C++
*/
#ifndef _DB_POOL_H_
#define _BD_POOL_H_
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/exception.h>
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/resultset.h>
#include <cppconn/prepared_statement.h>
#include <cppconn/statement.h>
#include <pthread.h>
#include <list>
using namespace std;
using namespace sql;
class DBPool
{
public:
// Singleton
static DBPool& GetInstance();
//init pool
void initPool(std::string url_, std::string user_, std::string password_, int maxSize_);
//get a conn from pool
Connection* GetConnection();
//put the conn back to pool
void ReleaseConnection(Connection *conn);
~DBPool();
private:
DBPool(){}
//init DB pool
void InitConnection(int initSize);
// create a connection
Connection* CreateConnection();
//destory connection
void DestoryConnection(Connection *conn);
//destory db pool
void DestoryConnPool();
private:
string user;
string password;
string url;
int maxSize;
int curSize;
Driver* driver; //sql driver (the sql will free it)
list<Connection*> connList; //create conn list
//thread lock mutex
static pthread_mutex_t lock;
};
#endif
cpp文件
/*
time: 2017.3.31
author: chenxun [email protected]
notice: MySql Connector C++
*/
#include <iostream>
#include <stdexcept>
#include <exception>
#include <stdio.h>
#include "DBPool.h"
using namespace std;
using namespace sql;
pthread_mutex_t DBPool::lock = PTHREAD_MUTEX_INITIALIZER;
//Singleton: get the single object
DBPool& DBPool::GetInstance()
{
static DBPool instance_;
return instance_;
}
void DBPool::initPool(std::string url_, std::string user_, std::string password_, int maxSize_)
{
this->user = user_;
this->password = password_;
this->url = url_;
this->maxSize = maxSize_;
this->curSize = 0;
try{
this->driver=sql::mysql::get_driver_instance();
}
catch(sql::SQLException& e)
{
perror("Get sql driver failed");
}
catch(std::runtime_error& e)
{
perror("Run error");
}
this->InitConnection(maxSize/2);
}
//init conn pool
void DBPool::InitConnection(int initSize)
{
Connection* conn;
pthread_mutex_lock(&lock);
for(int i =0;i <initSize; i++)
{
conn= this->CreateConnection();
if(conn)
{
connList.push_back(conn);
++(this->curSize);
}
else
{
perror("create conn error");
}
}
pthread_mutex_unlock(&lock);
}
Connection* DBPool::CreateConnection()
{
Connection* conn;
try{
conn = driver->connect(this->url,this->user,this->password); //create a conn
return conn;
}
catch(sql::SQLException& e)
{
perror("link error");
return NULL;
}
catch(std::runtime_error& e)
{
perror("run error");
return NULL;
}
}
Connection* DBPool::GetConnection()
{
Connection* conn;
pthread_mutex_lock(&lock);
if(connList.size()>0)//the pool have a conn
{
conn = connList.front();
connList.pop_front();//move the first conn
if(conn->isClosed())//if the conn is closed, delete it and recreate it
{
delete conn;
conn = this->CreateConnection();
}
if(conn == NULL)
{
--curSize;
}
pthread_mutex_unlock(&lock);
return conn;
}
else
{
if(curSize< maxSize)//the pool no conn
{
conn = this->CreateConnection();
if(conn)
{
++curSize;
pthread_mutex_unlock(&lock);
return conn;
}
else
{
pthread_mutex_unlock(&lock);
return NULL;
}
}
else //the conn count > maxSize
{
pthread_mutex_unlock(&lock);
return NULL;
}
}
}
//put conn back to pool
void DBPool::ReleaseConnection(Connection *conn)
{
if(conn)
{
pthread_mutex_lock(&lock);
connList.push_back(conn);
pthread_mutex_unlock(&lock);
}
}
void DBPool::DestoryConnPool()
{
list<Connection*>::iterator iter;
pthread_mutex_lock(&lock);
for(iter = connList.begin(); iter!=connList.end(); ++iter)
{
this->DestoryConnection(*iter);
}
curSize=0;
connList.clear();
pthread_mutex_unlock(&lock);
}
void DBPool::DestoryConnection(Connection* conn)
{
if(conn)
{
try{
conn->close();
}
catch(sql::SQLException&e)
{
perror(e.what());
}
catch(std::exception& e)
{
perror(e.what());
}
delete conn;
}
}
DBPool::~DBPool()
{
this->DestoryConnPool();
}
main.cpp文件
/*
* main.cpp
*
* Created on: 2013-3-26
* Author: chenxun
*/
#include "DBPool.h"
#include <stdio.h>
/*--------------------------------------------------------------
單例模式,全局唯一 db pool,程序中使用onnpool中獲取一個
db連接使用,使用完之後調用ReleaseConnection把conn放回pool中去.
----------------------------------------------------------------*/
DBPool connpool = DBPool::GetInstance();
int main(int argc, char* argv[])
{
//初始化連接,創建參數中maxSize一半的連接
connpool.initPool("tcp://127.0.0.1:3306", "root", "123456", 100);
Connection *con;
Statement *state;
ResultSet *result;
con = connpool.GetConnection();//get a db conn
for(int i = 0; i<1000; i++)
{
state = con->createStatement();
state->execute("use mysql");
// 查詢
result = state->executeQuery("select host,user from user");
// 輸出查詢
while (result->next())
{
try{
string user = result->getString("user");
string name = result->getString("host");
cout << user << " : " << name << endl;
}catch(sql::SQLException& e){
std::cout << e.what() << std::endl;
}
}
/*result = state->executeQuery("select cust_id,cust_name from customers");
while (result->next())
{
try{
string user = result->getString("cust_id");
string name = result->getString("cust_name");
cout << user << " : " << name << endl;
}catch(sql::SQLException& e){
std::cout << e.what() << std::endl;
}
}
*/
std::cout << i << std::endl;
}
delete result;
delete state;
connpool.ReleaseConnection(con);
return 0;
}
自己根據項目寫一個makefile,我這裏就不提供了。運行情況如下
注意include文件和so文件路勁編譯選項加上-lmysqlcppconn例如:
g++ main.cpp -o main -I... -L... –lmysqlcppconn
-I接你的mysqlconnector c++文頭
-L接你的msyqlconnector c++ lib路勁
運行情況如下: