C++ 操作sqlite3数据库

来源:互联网 发布:吉视传媒网络宽带连接 编辑:程序博客网 时间:2024/06/05 07:41
C++ 操作sqlite3数据库
参考:
http://www.cnblogs.com/BoyXiao/archive/2012/03/31/2426495.html
http://blog.csdn.net/harry_lyc/article/details/6663176
http://www.sqlite.org/download.html
http://www.th7.cn/Program/cp/201209/91677.shtml
#include "stdafx.h"#include "sqlite3.h" #include <iostream> #include <sstream> using namespace std; sqlite3 * pDB; int createTable() {         char* errMsg;         std::string dropTab = "drop table test_tab;";         string strSQL= "create table test_tab (f1 int, f2 long);";             int res= sqlite3_exec(pDB , dropTab.c_str() , 0 , 0 , &errMsg);             if (res != SQLITE_OK)         {                 std::cout << "执行SQL 出错." << errMsg << std::endl;                 return -1;         }         res = sqlite3_exec(pDB , strSQL.c_str() ,0 ,0, &errMsg);             if (res != SQLITE_OK)         {                 std::cout << "执行创建table的SQL 出错." << errMsg << std::endl;                 return -1;         }         else         {                 std::cout << "创建table的SQL成功执行."<< std::endl;         }             return 0; }     int insert1() {         char* errMsg;             int res = sqlite3_exec(pDB,"begin transaction;",0,0, &errMsg);             for (int i= 1; i < 10; ++i)         {                 std::stringstream strsql;                 strsql << "insert into test_tab    values(";                 strsql    << i << ","<< (i+10) << ");";                 std::string str = strsql.str();                 res = sqlite3_exec(pDB,str.c_str(),0,0, &errMsg);                 if (res != SQLITE_OK)                 {                         std::cout << "执行插入SQL 出错." << errMsg << std::endl;                         return -1;                 }         }             res = sqlite3_exec(pDB,"commit transaction;",0,0, &errMsg);             std::cout << "插入SQL成功执行."<< std::endl;                 return 0; }     static int callback(void *NotUsed, int argc, char **argv, char **azColName) {                 for(int i = 0 ; i < argc ; i++)         {                 std::cout << azColName[i] << " = " << (argv[i] ? argv[i] : "NULL") << ", " ;         }             std::cout<< "\n";         return 0; }     int select1() {         char* errMsg;         string strSQL= "select * from test_tab;";             int res = sqlite3_exec(pDB, strSQL.c_str(), callback , 0 , &errMsg);             if (res != SQLITE_OK)         {                 std::cout << "执行查询SQL 出错." << errMsg << std::endl;                 return -1;         }         else         {                 std::cout << "SQL查询成功执行."<< std::endl;         }             return 0; }     int main() {         int res = sqlite3_open("D:\\sql.db", &pDB);             if( res ){                 std::cout << "Can't open database: "<< sqlite3_errmsg(pDB);                 sqlite3_close(pDB);                 return -1;         }         res = createTable();         if (res != 0)         {                 return 0;         }         res = insert1();         if (res != 0)         {                 return 0;         }         select1();         return 0; }

原创粉丝点击