Cocos2d-X 学习笔记 18 Cocos2dx 下对sqlite3 的简单封装

DBUtil.h:

[cpp]  view plain copy
  1. class DBUtil{  
  2.   
  3.       
  4. public:  
  5.       
  6.     /************************************************************ 
  7.             封装 <a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=sqlite3&ie=utf-8&src=se_lighten_f">sqlite3</a>操作 
  8.      ************************************************************/  
  9.     //用来创建一个db数据库 db为数据库的名字  
  10.     //  打开数据库  
  11.     static void initDB(const char *db);  
  12.       
  13.     //用来判断表格是否存在  
  14.     // name:表示表名  
  15.     static bool tableIsExist(string name);  
  16.       
  17.       
  18.     //用来创建一个表名为name的表格,创建时会先匹配时否有该表的存在如果存在则不创建  
  19.     //创建表  
  20.     static void createTable(string sql,string name);  
  21.       
  22.     //用来删除一张表名为name的表格,删除时会先匹配是否有该表的存在如果不存在则不执行删除操作  
  23.     //删除表名  
  24.     static void deleteTable(string sql,string name);  
  25.       
  26.     //用来向表中插入一条数据  
  27.     //插入一条数据  
  28.     static void insertData(string sql);  
  29.       
  30.     //用来向表中删除一条数据  
  31.     //删除一条数据  
  32.     static void deleteData(string sql);  
  33.       
  34.     //用来向表中修改一条数据  
  35.     // 修改一条数据  
  36.     static void updateData(string sql);  
  37.       
  38.     //获取一个记录的条数  
  39.     // 获得记录的条数  
  40.     static int getDataCount(string sql);  
  41.       
  42.     //读取一条记录的信息  
  43.     /* 
  44.      *  此方法是查询方法,相当之重要,pSender最好是个vector 
  45.      */  
  46.     static void getDataInfo(string sql,void *pSend);  
  47.       
  48.     //关闭打开的数据库  
  49.     static void closeDB();  
  50.   
  51. };  

DBUtil.cpp:

[cpp]  view plain copy
  1. sqlite3 *pDB = NULL;//数据库指针  
  2. char * errMsg = NULL;//错误信息  
  3. std::string sqlstr;//SQL指令  
  4. int result;//sqlite3_exec返回值  
  5.   
  6.   
  7. //创建数据库  
  8. void DBUtil::initDB(const char *db )  
  9. {  
  10.     //打开一个数据库,如果该数据库不存在,则创建一个数据库文件  
  11.     result = sqlite3_open(db, &pDB);  
  12.     if( result != SQLITE_OK )  
  13.         CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  14. }  
  15.   
  16.   
  17. //tableIsExist的<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0&ie=utf-8&src=se_lighten_f">回调函数</a>  
  18. int isExisted( void * para, int n_column, char ** column_value, char ** column_name )  
  19. {  
  20.     bool *isExisted_=(bool*)para;  
  21.     *isExisted_=(**column_value)!='0';  
  22.     return 0;  
  23. }  
  24.   
  25.   
  26.   
  27. //判断表格是否存在  
  28. bool DBUtil::tableIsExist( string name )  
  29. {  
  30.     if (pDB!=NULL)  
  31.     {  
  32.         //判断表是否存在  
  33.         bool tableIsExisted;  
  34.         sqlstr = "select count(type) from sqlite_master where type='table' and name ='"+name+"'";  
  35.         result =sqlite3_exec(pDB,sqlstr.c_str(),isExisted,&tableIsExisted,&errMsg);  
  36.         return tableIsExisted;  
  37.     }  
  38.     return false;  
  39. }  
  40.   
  41.   
  42.   
  43. //在数据库中判断名为name的表示否存在,如果不存在则创建这张表  
  44. //@示例语句string sqls = "create table user(id integer,username text,password text)";  
  45. void DBUtil::createTable( string sql,string name )  
  46. {  
  47.     if (!tableIsExist(name))  
  48.     {  
  49.         //创建表,设置ID为主键,且自动增加  
  50.         result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMsg);  
  51.         if( result != SQLITE_OK )  
  52.             CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  53.     }  
  54.       
  55. }  
  56.   
  57.   
  58. //删除表格  
  59. //@示例语句sqlstr="drop table name";  
  60. void DBUtil::deleteTable( string sql,string name )  
  61. {  
  62.     if (tableIsExist(name))  
  63.     {  
  64.         result = sqlite3_exec(pDB,sql.c_str(),NULL,NULL,&errMsg);  
  65.         if( result != SQLITE_OK )  
  66.             CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  67.     }  
  68. }  
  69.   
  70.   
  71. //插入数据  
  72. //@示例语句sqlstr=" insert into MyTable_1( name ) values ( '<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E6%93%8E%E5%A4%A9%E6%9F%B1&ie=utf-8&src=se_lighten_f">擎天柱</a>' ) ";  
  73. void DBUtil::insertData( string sql ){  
  74.     result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );  
  75.     if(result != SQLITE_OK )  
  76.         CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  77. }  
  78.   
  79.   
  80. //删除数据  
  81. //@示例语句sqlstr="delete from MyTable_1 where ID = 2";  
  82. void DBUtil::deleteData( string sql )  
  83. {  
  84.     result=sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );  
  85.     if(result != SQLITE_OK )  
  86.         CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  87. }  
  88.   
  89.   
  90. //修改数据  
  91. //@示例语句        sqlstr="update MyTable_1 set name='威震天' where ID = 3";  
  92. void DBUtil::updateData( string sql )  
  93. {  
  94.     result = sqlite3_exec( pDB, sql.c_str() , NULL, NULL, &errMsg );  
  95.     if(result != SQLITE_OK )  
  96.         CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg );  
  97. }  
  98.   
  99.   
  100. //getDataCount的回调函数  
  101. int loadRecordCount( void * para, int n_column, char ** column_value, char ** column_name )  
  102. {  
  103.     int *count=(int*)para;  
  104.     *count=n_column;  
  105.     return 0;  
  106. }  
  107. //获取记录的条数  
  108. //@示例语句string sqlsssss = "select count(*) from user";  
  109. //@示例语句  取得表格字段的语句string sqlsssss = "select * from user";  
  110. int DBUtil::getDataCount( string sql )  
  111. {  
  112.     int count=0;  
  113.     sqlite3_exec( pDB, sql.c_str() , loadRecordCount, &count, &errMsg );  
  114.     return count;  
  115. }  
  116.   
  117.   
  118. //getDataInfo的回调函数  
  119. int loadRecord( void * para, int n_column, char ** column_value, char ** column_name )  
  120. {  
  121.     CCLOG("n_column:%d",n_column);  
  122.       
  123. //    TestVO* testVO = (TestVO*)para;  
  124.       
  125.  //   testVO->mId = atoi(column_value[0]);  
  126.  //   testVO->level = atoi(column_value[1]);  
  127. //    testVO->lastscore = atoi(column_value[2]);  
  128.  //  testVO->bestscore = atoi(column_value[3]);  
  129.  //  testVO->star = atoi(column_value[4]);  
  130.       
  131.       
  132.     /* 可能有5个字段 */  
  133.     // id level lastscore bestscore star  
  134. //    CCLOG("c[0]:%s,c[1]:%s,c[2]:%s,c[3]:%s,c[4]:%s",column_name[0],column_name[1],column_name[2],column_name[3],column_name[4]);  
  135. //      
  136.     CCLog("id=%s,level=%s,lastscore=%s,bestscore=%s,star=%s",column_value[0],column_value[1],column_value[2],column_value[3],column_value[4]);  
  137.     return 0;  
  138. }  
  139. //获取一条记录的信息 其中的pSend是一个<a target="_blank" style="color: #0000F0; display:inline; position:static; background:none;" href="http://www.so.com/s?q=%E5%AE%9E%E4%BD%93%E7%B1%BB&ie=utf-8&src=se_lighten_f">实体类</a>我们以后可以自定义一个继承了CCObject的类来代替他保存数据库中取出来的数据  
  140. /* 
  141.  *  这里最好扩展下,让  pSend  是一个vector 
  142.  */  
  143. void DBUtil::getDataInfo( string sql,void *pSend )  
  144. {  
  145.     sqlite3_exec( pDB, sql.c_str() , loadRecord, pSend, &errMsg );  
  146. }  
  147.   
  148.   
  149. //关闭数据库  
  150. void DBUtil::closeDB()  
  151. {  
  152.     sqlite3_close(pDB);  
  153. }  


测试:

[cpp]  view plain copy
  1.  // Test Database  
  2.     string fullDBPath = CCFileUtils::sharedFileUtils()->getWriteablePath() + "save.db";  
  3.       
  4.     CCLOG("fullPath : %s",fullDBPath.c_str());  
  5.       
  6.     //打开数据库  
  7.     DBUtil::initDB(fullDBPath.c_str());  
  8.       
  9.     string createTableSql = "create table zuma (id integer primary key autoincrement,level integer,lastscore integer,bestscore integer,star integer);";  
  10.     DBUtil::createTable(createTableSql.c_str(),"zuma");  
  11.       
  12.      //向表格中插入数据  
  13.     string sqlss = "insert into zuma (level,lastscore,bestscore,star) values (100,100,500,1)";  
  14.       
  15.     /* 插入一条数据 */  
  16.     DBUtil::insertData(sqlss);  
  17.      
  18.     // 更新  
  19.     string updateString = "update zuma set star = 5;";  
  20.       
  21.     DBUtil::updateData(updateString);  
  22.       
  23.     /* 查询数据 */  
  24.     string selectStr = "select * from zuma";  
  25. //    CCObject* pp = new CCObject();  
  26. //    DBUtil::getDataInfo(selectStr, pp);  
  27.       
  28.     TestVO* testVO = new TestVO();  
  29.     testVO->mId = 1111;  
  30.       
  31.       
  32.     DBUtil::getDataInfo(selectStr,testVO);  
  33.       
  34.     /* 显示结果 */  
  35.     CCLOG("id:%d",testVO->mId);  
  36.     CCLOG("level:%d",testVO->level);  
  37.     CCLOG("lastscore:%d",testVO->lastscore);  
  38.     CCLOG("bestscore:%d",testVO->bestscore);  
  39.     CCLOG("star:%d",testVO->star);  
  40.       
  41.     delete testVO;  
  42.       
  43.     /* 不能忘记关闭数据库 */  
  44.     DBUtil::closeDB();  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值