SQLiteC++Wrapper是一个C++语言对SQLite的最小封装包。
示例代码1:
#include<string>#include<iostream>#include<stdexcept>usingnamespacestd;#include"sqlite3x.hpp"usingnamespacesqlite3x;intmain(void){ try{ sqlite3_connectioncon("test.db"); intcount=con.executeint( "selectcount(*)" "fromsqlite_master" "wherename='t_test';"); if(count==0){ con.executenonquery( "createtablet_test(number,string);"); } sqlite3_transactiontrans(con); { sqlite3_commandcmd(con, "insertintot_testvalues(?,?);"); cmd.bind(2,"foobar",6); for(inti=0;i<10000;++i){ cmd.bind(1,i); cmd.executenonquery(); } } //iftransgoesoutofscope(duetoanexceptionor //anythingelse)beforecallingcommit(),itwill //automaticallyrollback() trans.commit(); } catch(exception&ex){ cerr<<"ExceptionOccured:"<<ex.what()<<endl; } return0;}示例代码2:
#include<iostream>#include<stdexcept>usingnamespacestd;#include"sqlite3x.hpp"usingnamespacesqlite3x;intmain(void){ try{ sqlite3_connectioncon("test.db"); sqlite3_commandcmd(con,"select*fromt_test;"); sqlite3_readerreader=cmd.executereader(); while(reader.read()){ cout<<reader.getcolname(0)<<":" <<reader.getint(0)<<endl; } } catch(exception&ex){ cerr<<"ExceptionOccured:"<<ex.what()<<endl; } return0;}
评论