MySql C++調用庫Connector/c++編譯 和 接口封裝【二】Connector/c++編譯

2、Connector/c++庫的編譯:
    一、把MySql數據庫安裝完成後,把bin目錄加入環境變量。
    
    二、下載boost庫,官網就有下載:  http://www.boost.org/
 
    三、安裝cmake工具,加入環境變量。
        命令行: cmake -G   , 列出cmake支持生成工程文件的各個版本。
 
    四、下載 Connector/c++ 源文件 : http://dev.mysql.com/downloads/connector/cpp/
  Select Platform: 下拉選項選擇 Source Code 。這裏貌似要FQ才行。
  也能夠到這裏下載: http://download.csdn.net/detail/biantaiwangzi/9092595
  

 1、生成工程文件

 解壓Connector/c++ ,命令行 cd 到解壓目錄 。我編譯的是2010版,命令以下:
 
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;
}
相關文章
相關標籤/搜索