Cocos2d-X之游戏存储Sqlite基础篇四

正在做的项目需要sqlite数据库存储数据。小巧 、高效和易操作是sqlite的明显特点,无平台更是强大。开源且免费啊,亲。

好的,下面步入正题。看下xcode下的Cocos2d—X的数据存储如何使用。

看下sqlite表的数据返回,会带有字段的一行:

As an example of the result table format,suppose a query result is as follows:

Name        | Age
-----------------------
Alice       | 43
Bob         | 28
Cindy       | 21

There are two column (M==2) and three rows (N==3). Thus the result table has 8 entries. Suppose the result table is stored in an array names azResult. Then azResult holds this content:

azResult[0] = "Name";
azResult[1] = "Age";
azResult[2] = "Alice";
azResult[3] = "43";
azResult[4] = "Bob";
azResult[5] = "28";
azResult[6] = "Cindy";
azResult[7] = "21";


在helloWorld的初始化直接写了。废话不说,直接代码贴出:


access函数判断文件存在与否,mode为0.

  1. stringpath=CCFileUtils::sharedFileUtils()->getWritablePath()+"MysqLite.db";
  2. remove(path.c_str());
  3. intflag=access(path.c_str(),0);
  4. if(flag!=0){
  5. userData.init();
  6. }



copy
    #include<sqlite3.h>

copy
    voidHelloWorld::init()
  1. {
  2. //1.创建数据库testsqlite3
  3. sqlite3*testsqlite3=NULL;
  4. //sqlite3_open()方法会打开数据库,没有就自动创建一个
  5. intresultOK=sqlite3_open("/Users/kevin/Desktop/testsqlite9.db",&testsqlite3);
  6. //返回数字0,说明创建成功
  7. if(resultOK!=sqlITE_OK){
  8. sqlite3_close(testsqlite3);
  9. return;
  10. }
  11. cclOG("resultOK%d",resultOK);
  12. //2.使用sql语句创建表testTable,并设置字段
  13. constchar*createTablesql="createtabletestTable(int_colINTERGER,float_colREAL,string_colTEXT,name_colTEXT)";
  14. //stmt,即是statement句柄,承载着sql语句
  15. sqlite3_stmt*stmt=NULL;
  16. //sql语句的长度
  17. intlength=strlen(createTablesql);
  18. cclOG("length%d",length);
  19. //sqlite3_prepare_v2,准备创建数据表,参数分别是数据库名称,表名称,语句长度,句柄。如果写入错误,则释放stmt对象,关闭数据库
  20. if(sqlite3_prepare_v2(testsqlite3,createTablesql,length,&stmt,NULL)!=sqlITE_OK){
  21. if(stmt){
  22. sqlite3_finalize(stmt);
  23. sqlite3_close(testsqlite3);
  24. return;
  25. }
  26. cclOG("%d:准备执行语句成功",sqlite3_prepare_v2(testsqlite3,NULL));
  27. //sqlite3_step执行创建语句,如果创建错误,则释放stmt对象,关闭数据库
  28. if(sqlite3_step(stmt)!=sqlITE_DONE){
  29. //cclOG("sqlite3_step(stmt)%d",sqlite3_step(stmt));
  30. //释放创建表语句的对象内存
  31. cclOG("createthetablesuccessed!");
  32. //3.insert表数据
  33. char*insertDatabase="insertintotestTablevalues(100,100,'这是一个测试','我的名字叫kevin')";
  34. sqlite3_stmt*stmt4;
  35. if(stmt4){
  36. sqlite3_finalize(stmt4);
  37. intres1=sqlite3_step(stmt4);
  38. if(res1!=sqlITE_DONE){
  39. sqlite3_finalize(stmt4);
  40. cclOG("插入数据成功");
  41. //成功后清除句柄对象
  42. //4.使用SQL查询语句
  43. char*selectsql="select*fromtestTable";
  44. sqlite3_stmt*stmt2=NULL;
  45. intlength2=strlen(selectsql);
  46. cclOG("length2%d",length2);
  47. if(stmt2){
  48. sqlite3_finalize(stmt2);
  49. intres=sqlite3_step(stmt2);
  50. cclOG("res%d",res);
  51. intfieldCount=sqlite3_column_count(stmt2);
  52. cclOG("sqlite3_column_count(stmt2)fieldCount:%d",fieldCount);
  53. if(res==sqlITE_ROW){
  54. for(intcount=0;count<fieldCount;count++){
  55. intstype=sqlite3_column_type(stmt2,count);
  56. if(stype==sqlITE_INTEGER){
  57. inttemp1=sqlite3_column_int(stmt2,248)"> cclOG("temp1%d",temp1);
  58. if(stype==sqlITE_FLOAT){
  59. doubletemp2=sqlite3_column_double(stmt2,248)"> cclOG("temp2%.2f",temp2);
  60. if(stype==sqlITE_TEXT){
  61. char*temp3=(char*)sqlite3_column_text(stmt2,248)"> cclOG("temp3%s",temp3);
  62. if(stype==sqlITE_NULL){
  63. cclOG("NULL");
  64. }elseif(res==sqlITE_DONE)
  65. {
  66. cclOG("查询已经完成");
  67. //5.使用droptable模块
  68. char*deleteDatabase="droptabletestTable";
  69. sqlite3_stmt*stmt3;
  70. sqlITE_OK){
  71. if(stmt3){
  72. sqlite3_finalize(stmt3);
  73. if(sqlite3_step(stmt3)==sqlITE_DONE){
  74. cclOG("dropthetablesucceed");
  75. cclOG("sqlite3_step(stmt3)%d",sqlite3_step(stmt3));
  76. cclOG("Hellosqlite!");
  77. }


看下输出结果:


copy
    Cocos2d:resultOK0
  1. Cocos2d:length88
  2. Cocos2d:0:准备执行语句成功
  3. Cocos2d:createthedatabasesuccessed!
  4. Cocos2d:插入数据成功
  5. Cocos2d:length224
  6. Cocos2d:rES100
  7. Cocos2d:sqlite3_column_count(stmt2)fieldCount:4
  8. Cocos2d:temp1100
  9. Cocos2d:temp2100.00
  10. Cocos2d:temp3这是一个测试
  11. Cocos2d:temp3我的名字叫kevin
  12. Cocos2d:dropthetablesucceed
  13. Cocos2d:sqlite3_step(stmt3)21
  14. Cocos2d:Hellosqlite!


原文地址:http://www.jb51.cc/article/p-vsqehcsj-be.html

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...