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

posted @ 2016-02-19 14:28  onemuji  阅读(2260)  评论(0编辑  收藏  举报