cmake -G "Visual Studio 10 2010" -DBOOST_ROOT:STRING="E:\GitHub"
-DBOOST_ROOT:STRING 指定 boost库所在目录
执行完后,就获得sln工程文件。我这里获得的是:MYSQLCPPCONN.sln
2、编译
打开工程文件,编译 MYSQLCPPCONN 方案。 在 mysql-connector-c++-1.1.6\driver\Debug 目录下就能够获得 dll 和 lib文件。
2.一、使用 mysqlcppconn.dll 你须要 libmysql.dll 库文件。
这个文件你能够在 mysql的安装目录 C:\Program Files\MySQL\MySQL Server 5.6\lib 下找到。
2.二、工程中的其余方案,都是 Connector/C++ 的使用例子。做为参考很是有用,建议你们看一下。
3、库的使用
库使用须要几个文件:
一、include 文件夹 c/c++ /常规/附加包含目录
Connector/c++ 的安装版里面的Include 文件夹。或者把 /driver以及/driver/nativeapi 里面的头文件拷贝到一个文件夹里面(注意nativeapi要更名为 cppconn)。
二、Connector/c++ 库文件 和 MySql库文件:
2.一、mysqlcppconn.dll /debug,exe生成目录
2.二、mysqlcppconn.lib 连接器/输入/附加依赖项
2.三、libmysql.dll /debug
三、boost库所在目录 c/c++/常规/附加包含目录
4、使用例子
先建数据库:
create database test;
建表:mysql
use test; create table test(id int ,name varchar(32));
插入数据:c++
insert into test.test value(1001,"sanyue"); insert into test.test value(1002,"sixbeauty"); insert into test.test value(1003,"chouwa");
建立存储过程:sql
use test; delimiter $ create procedure testproc1 (nId int) --注意这里和select那句的 ";" begin select id,name from test where id = nId; end $ delimiter ;
例子:数据库
#include "mysql_connection.h" #include "mysql_driver.h" #include "cppconn/prepared_statement.h" #include "stdlib.h" using namespace std; typedef boost::scoped_ptr<sql::mysql::MySQL_Driver> MySQL_Driver; typedef boost::scoped_ptr<sql::Connection> Connection; typedef boost::scoped_ptr<sql::PreparedStatement> PreparedStatement; typedef boost::scoped_ptr<sql::Statement> Statement; typedef boost::scoped_ptr<sql::ResultSet> ResultSet; #define CRTDBG_MAP_ALLOC int main() { sql::mysql::MySQL_Driver *driver; Connection con(NULL); PreparedStatement prepareState(NULL); ResultSet result(NULL); //初始化驱动 try { driver=(sql::mysql::get_mysql_driver_instance()); //创建链接 con.reset(driver->connect("tcp://127.0.0.1:3306","root","123456")); if(con->isValid() == false) { cout<<"链接失败"<<endl; return -1; } // 查询存储过程 prepareState.reset(con->prepareStatement("call test.testproc1(?)")); prepareState->setInt(1,1001); prepareState->executeUpdate(); result.reset(prepareState->getResultSet()); // 输出结果 while(result->next()) { int id = result->getInt("id"); string name = result->getString("name"); cout<<"testuser: "<< id <<" , "<<name<<endl; } result->close(); prepareState->close(); con->close(); /* result.reset(NULL); prepareState.reset(NULL); con.reset(NULL); */ _CrtDumpMemoryLeaks(); //_CrtDumpMemoryLeaks(); } catch (sql::SQLException &e) { cout<<e.what()<<",state:"<<e.getSQLState()<<endl; cout<<"errorCode: " << e.getErrorCode()<<endl; } return 0; }