2014年01月27日 星期一 11:07
CppDB项目是CppCMS的一个子项目,提供了多种数据库的封装,统一的API非常简洁易用,同时,CppDB还提供Connection Pool,Statement Cache等高级特性,对于高性能C++应用程序非常有实际意义。
代码示例如下:
#include <cppdb/frontend.h> #include <iostream> #include <ctime> using namespace std; int main(int argc,char **argv) { try { string connstr="mysql:host=10.1.1.161;"; connstr += "user=mengguang;password=1234qwer;"; connstr += "opt_reconnect=1;database=test;@pool_size=3"; cppdb::session ses(connstr); cppdb::statement stat=ses << "insert into friends (name,email) values (?,?)" << "mengguang" << "mengguang@gmail.com"; stat.exec(); cout << "Affect rows: " << stat.affected() << endl; cout << "Last Insert ID: " << stat.last_insert_id() << endl; stat.reset(); stat.bind("menghui"); stat.bind("menghui@163.com"); stat.exec(); cout << "Affect rows: " << stat.affected() << endl; cout << "Last Insert ID: " << stat.last_insert_id() << endl; cppdb::result res=ses << "select * from friends"; int id; string name; string email; cout << "Cols: " << res.cols() << endl; while(res.next()) { res >> id >> name >> email; cout << id << " " << name << " " << email << endl; } res=ses << "select email from friends where name=? limit 1" << "mengguang" << cppdb::row; if(!res.empty()) { cout << "email: " << res.get<string>("email") << endl; } cppdb::transaction guard(ses); cppdb::statement clean=ses << "delete from friends"; clean.exec(); cout << "Deleted rows: " << clean.affected() << endl; guard.commit(); } catch (exception &e) { cerr << "ERROR: " << e.what() << endl; return -1; } return 0; }
参考资料:
Zeuux © 2024
京ICP备05028076号