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

前端之家收集整理的这篇文章主要介绍了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. @H_403_48@stringpath=CCFileUtils::sharedFileUtils()->getWritablePath()+"MysqLite.db"@H_403_48@;
  2. remove(path.c_str());
  3. int@H_403_48@flag=access(path.c_str(),0);
  4. if@H_403_48@(flag!=0){
  5. userData.init();
  6. }



copy
    #include<sqlite3.h>@H_403_48@

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


看下输出结果:


copy
    @H_403_48@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

猜你在找的Cocos2d-x相关文章