CppDB使用示例

CppDB是一个高性能的数据库访问库,比MySQL++快很多(我只是使用了MySQL++的默认设置)。

下面的例子代码演示了如何使用CppDB访问数据库。

#include <iostream>
using namespace std;
#include <cppdb/frontend.h>

int main(int argc,char* argv[]){
	try{
		string connection_string("mysql:host=192.168.1.15;database=d01;user=data;password=skst;set_charset_name=utf8");
		cppdb::session session(connection_string);
		string sql="select address from tape_local limit 0,1";
		cppdb::result res = session<<sql;
		while(res.next()){
			cout<<res.get<string>("address")<<endl;
		}
	}catch(std::exception const& ex){
		cout<<ex.what()<<endl;
	}catch(...){
		cout<<"Unknown exception"<<endl;
	}
}

特别注意 在连接字符串中使用set_charset_name=utf8,否则读取出来的中文是乱码。


你可能感兴趣的:(cpp)