当前位置:Gxlcms > 数据库问题 > c++调用SQLite3的常用使用方法;

c++调用SQLite3的常用使用方法;

时间:2021-07-01 10:21:17 帮助过:2人阅读

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

 

c++调用SQLite3的常用使用方法;

标签:

人气教程排行