C++ 操作sqlite3資料庫

pengfoo發表於2013-09-25
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; 
}


相關文章