MySQL++簡單使用記錄.md


#1.簡介

MySQL++ is a powerful C++ wrapper for MySQL’s C API. Its purpose is to make working with queries as easy as working with STL containers.

 

#2.編譯安裝

下載MySQL++安裝包 http://www.tangentsoft.net/mysql++/releases/mysql++-3.2.2.tar.gz

##2.1.ubuntu

###安裝libmysqlclient
sudo apt-get install libmysqlclient-dev

locate libmysqlclient 從結果中可用看到libmysqlclient.so在/usr/lib/x86_64-linux-gnu/目錄下

頭文件:/usr/include/mysql

lib: /usr/lib/x86_64-linux-gnu

###編譯
sudo ./configure --with-mysql-lib=/usr/lib/x86_64-linux-gnu
sudo make
sudo make install

/usr/local/include/mysql++
/usr/local/lib/mysqlpp.so

頭文件:/usr/local/include/mysql++

lib: /usr/local/lib

###so的配置
/etc/ld.so.conf /usr/local/lib

 

##2.2.centos

###安裝libmysqlclient
yum install mysql-devel
rpm -ql mysql-devel 從結果中可以看到libmysqlclient.so在/usr/lib64/mysql/目錄下

頭文件:/usr/include/mysql

lib:/usr/lib64/mysql/

###編譯
./configure --prefix=/usr/local --enable-thread-check --with-mysql-lib=/usr/lib64/mysql
make
make install

頭文件:/usr/local/include/mysql++

lib: /usr/local/lib
###so的配置
/etc/ld.so.conf /usr/local/lib

說明:mysql++的configure沒有使用--enable-thread-check選項,因為我打算自己來管理多線程。
關於MySQL++多線程相關的內容可以看看http://tangentsoft.net/mysql++/doc/html/userman/threads.html。

 

#3.使用

主要使用的類有mysqlpp::Connection和mysqlpp::Query,前者用於跟mysql的連接,后者主要用於執行SQL語句。

##3.1 連接

示例代碼:

bool throwExceptionOnError = false;
conn_ = new mysqlpp::Connection(throwExceptionOnError);

conn_->set_option(new mysqlpp::ReconnectOption(true));

bool success = conn_->connect(g_config->DbName_,
                                  g_config->DbServer_,
                                   g_config->DbUserName_,
                                  g_config->DbPassword_,
                                  g_config->DbPort_);
Connection

##3.2 查詢

MySql++支持三種查詢: Query::execute(), Query::store(), Query::use()

execute()用於不返回數據的查詢,該函數返回一個SimpleResult對象。如果只要成功與否的標識,可以使用Query::exec(),它返回一個bool值,標示執行成功與否。

store()用於用服務器獲取數據,該函數返回一個StoreQueryResult對象。對象包含了整個查詢結果,使用stl::map方式從里面取數據即可。

use()同樣用於從服務器獲取數據,不過該函數返回UseQueryResult對象。相比store()而言更節省內存,該對象類似StoreQueryResult,但是不提供隨機訪問的特性。use查詢會讓服務器一次返回結果集的一行。

Query對象的errnum()返回上次執行對應的錯誤代碼,error()返回錯誤信息,affected_rows()返回受影響的行數。

示例代碼:

query_ = new mysqlpp::Query(conn_, throwExceptionOnError, 0);

// exec()
bool    query_ = new mysqlpp::Query(conn_, throwExceptionOnError, 0);

// store()
mysqlpp::StoreQueryResult res = query_->store(sql, strlen(sql));
        int num = res.num_rows();
        if (num <= 0) {
            return false;
        }

        std::string a;
        std::string b;
        std::string c;
        for (int i=0; i<num; i++) {
            res[i]["a"].to_string(a);
            res[i]["b"].to_string(b);
            res[i]["c"].to_string(c);
}










        
Query

 

#參考:

1.MySQL++ v3.2.2 User Manual http://tangentsoft.net/mysql++/doc/html/userman/index.html
2.MySQL++ doc http://tangentsoft.net/mysql++/doc/html/refman/annotated.html
3.MYSQL c api http://dev.mysql.com/doc/refman/5.5/en/c-api-function-overview.html

4.http://www.cnblogs.com/comoon/p/4104482.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM