CppDB使用示例(转)

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

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

[cpp]

  1. #include <iostream>
  2. using namespace std; 
  3. #include <cppdb/frontend.h>
  4. int main(int argc,char* argv[]){ 
  5. try{ 
  6.         string connection_string("mysql:host=192.168.1.15;database=d01;user=data;password=skst;set_charset_name=utf8"); 
  7.         cppdb::session session(connection_string); 
  8.         string sql="select address from tape_local limit 0,1"; 
  9.         cppdb::result res = session<<sql; 
  10. while(res.next()){ 
  11.             cout<<res.get<string>("address")<<endl; 
  12.         } 
  13.     }catch(std::exception const& ex){ 
  14.         cout<<ex.what()<<endl; 
  15.     }catch(...){ 
  16.         cout<<"Unknown exception"<<endl; 
  17.     } 

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

 

转自:http://blog.csdn.net/sheismylife/article/details/6767770

posted @ 2012-04-12 16:22  Owen Wilson  阅读(889)  评论(0编辑  收藏  举报