下面測試用的sqlite例子;你們能夠參考使用;sql
1 1 #include "CppSQLite3.h" 2 2 3 3 Class TestSqlite{ 4 4 5 5 //定義db指針 6 6 private: 7 7 CppSQLite3DB* m_pSqlDb; 8 8 TestSqlite() 9 9 { 10 10 m_pSqlDb = NULL; 11 11 Init(); 12 12 } 13 13 14 14 ~TestSqlite() 15 15 { 16 16 if ( m_pSqlDb ) 17 17 { 18 18 m_pSqlDb.Close(); 19 19 delete m_pSqlDb; 20 20 m_pSqlDb = NULL; 21 21 } 22 22 } 23 23 //初始化 24 24 BOOL Init() 25 25 { 26 26 //初始化sqlite指針 27 27 if ( m_pSqlDb || !(m_pSqlDb = new CppSQLite3DB)) 28 28 { 29 29 return FALSE; 30 30 } 31 31 32 32 try 33 33 { 34 34 string strDbFile = "D:\\Chunk.s3db"; 35 35 m_pSqlDb->open( strDbFile.c_str() );//打開指定位置的本地數據庫 36 36 } 37 37 catch (CppSQLite3Exception& e)//處理sqlite異常 38 38 { 39 39 return FALSE; 40 40 } 41 41 42 42 return TRUE; 43 43 } 44 44 public: 45 45 //讀出db中指定名稱的表數據 46 46 void ReadAllLine(map<int,int>& mpDbInfo,const string &TblName) 47 47 { 48 48 try 49 49 { 50 50 char szCmd[256]; 51 51 sprintf( szCmd, "SELECT id,testnum FROM %s;",TblName); 52 52 CppSQLite3Query query = m_pSqlDb->execQuery( szCmd );//執行查詢語句 53 53 while(!query.eof()) 54 54 { 55 55 int id = query.getIntField( "id"); //列項爲id的值 56 56 int testnum = query.getIntField( "testnum"); //列項testnum的值 57 57 58 58 mpDbInfo.insert(make_pair(id,testnum));//插入map 59 59 query.nextRow();//繼續下一行 60 60 } 61 61 query.finalize();//結束查詢,釋放內存 62 62 } 63 63 catch (CppSQLite3Exception& e) 64 64 { 65 65 return; 66 66 } 67 67 } 68 68 69 69 //更新指定數據 70 70 BOOL DeleteLine(const string& TblName,const int& id,const int& num) 71 71 { 72 72 try 73 73 { 74 74 char szCmd[256]; 75 75 sprintf( szCmd, "update %s set num = %d WHERE id=%d;",TblName,num,id);//更新內容 76 76 m_pSqlDb->execDML( szCmd ); 77 77 } 78 78 catch (CppSQLite3Exception& e) 79 79 { 80 80 return FALSE; 81 81 } 82 82 return TRUE; 83 83 } 84 84 85 85 //刪除指定數據 86 86 BOOL DeleteLine(const string& TblName,const int& id) 87 87 { 88 88 try 89 89 { 90 90 char szCmd[256]; 91 91 sprintf( szCmd, "DELETE FROM %d WHERE id=%d;", TblName,id);//刪除語句 92 92 m_pSqlDb->execDML( szCmd ); 93 93 } 94 94 catch (CppSQLite3Exception& e) 95 95 { 96 96 return FALSE; 97 97 } 98 98 return TRUE; 99 99 } 100 100 }; 101 102