博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cocos2d-X之游戏存储Sqlite基础篇(四)
阅读量:4216 次
发布时间:2019-05-26

本文共 4675 字,大约阅读时间需要 15 分钟。

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

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

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

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

Name        | Age-----------------------Alice       | 43Bob         | 28Cindy       | 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.

[cpp] 
  1. string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"mySqlite.db";  
  2.       
  3.     remove(path.c_str());  
  4.       
  5.     int flag = access(path.c_str(), 0);  
  6.     if (flag != 0) {  
  7.         userData.init();  
  8.     }  


[cpp] 
  1. #include <sqlite3.h>  

[cpp] 
  1. void HelloWorld::init()  
  2. {  
  3.     //1.创建数据库testSqlite3  
  4.     sqlite3 *testSqlite3 = NULL;  
  5.     //sqlite3_open()方法会打开数据库,没有就自动创建一个  
  6.     int resultOK = sqlite3_open("/Users/kevin/Desktop/testSqlite9.db", &testSqlite3);  
  7.     //返回数字0,说明创建成功  
  8.     if (resultOK != SQLITE_OK) {  
  9.         sqlite3_close(testSqlite3);  
  10.         return;  
  11.     }  
  12.     CCLOG("resultOK  %d",resultOK);  
  13.       
  14.     //2.使用sql语句创建表testTable,并设置字段  
  15.     const char *createTableSQL = "create table testTable (int_col INTERGER ,float_col REAL ,string_col TEXT,name_col TEXT)";  
  16.     //stmt,即是state ment句柄,承载着sql语句  
  17.     sqlite3_stmt *stmt = NULL;  
  18.     //sql语句的长度  
  19.     int length = strlen(createTableSQL);  
  20.     CCLOG("length %d",length);  
  21.       
  22.     //sqlite3_prepare_v2,准备创建数据表,参数分别是数据库名称,表名称,语句长度,句柄。如果写入错误,则释放stmt对象,关闭数据库  
  23.     if (sqlite3_prepare_v2(testSqlite3, createTableSQL, length, &stmt, NULL)!= SQLITE_OK) {  
  24.         if (stmt) {  
  25.             sqlite3_finalize(stmt);  
  26.             sqlite3_close(testSqlite3);  
  27.             return;  
  28.         }  
  29.     }  
  30.     CCLOG("%d :准备执行语句成功",sqlite3_prepare_v2(testSqlite3, createTableSQL, length, &stmt, NULL));  
  31.     //sqlite3_step执行创建语句,如果创建错误,则释放stmt对象,关闭数据库  
  32.     if (sqlite3_step(stmt)!=SQLITE_DONE) {  
  33.         sqlite3_finalize(stmt);  
  34.         sqlite3_close(testSqlite3);  
  35.         return;  
  36.     }  
  37.     //CCLOG("sqlite3_step(stmt) %d",sqlite3_step(stmt));  
  38.     //释放创建表语句的对象内存  
  39.     sqlite3_finalize(stmt);  
  40.     CCLOG("create the table successed!");  
  41.       
  42.     //3.insert 表数据  
  43.     const char *insertDatabase = "insert into testTable values(100,100,'这是一个测试','我的名字叫kevin')";  
  44.     sqlite3_stmt *stmt4;  
  45.     if (sqlite3_prepare_v2(testSqlite3, insertDatabase, strlen(insertDatabase), &stmt4,NULL)!=SQLITE_OK) {  
  46.         if (stmt4) {  
  47.             sqlite3_finalize(stmt4);  
  48.         }  
  49.         sqlite3_close(testSqlite3);  
  50.         return;  
  51.     }  
  52.     int res1 = sqlite3_step(stmt4);  
  53.     if (res1 != SQLITE_DONE) {  
  54.         sqlite3_finalize(stmt4);  
  55.         sqlite3_close(testSqlite3);  
  56.         return;  
  57.     }  
  58.     CCLOG("插入数据成功");  
  59.     //成功后清除句柄对象  
  60.     sqlite3_finalize(stmt4);  
  61.       
  62.       
  63.       
  64.     //4.使用sql查询语句  
  65.     const char *selectSQL = "select * from testTable ";  
  66.     //stmt,即是state ment句柄,承载着sql语句  
  67.     sqlite3_stmt *stmt2 = NULL;  
  68.     //sql语句的长度  
  69.     int length2 = strlen(selectSQL);  
  70.     CCLOG("length2 %d",length2);  
  71.       
  72.     //sqlite3_prepare_v2,准备创建数据表,参数分别是数据库名称,表名称,语句长度,句柄。如果写入错误,则释放stmt对象,关闭数据库  
  73.     if (sqlite3_prepare_v2(testSqlite3, selectSQL, length2, &stmt2, NULL)!= SQLITE_OK) {  
  74.         if (stmt2) {  
  75.             sqlite3_finalize(stmt2);  
  76.             sqlite3_close(testSqlite3);  
  77.             return;  
  78.         }  
  79.     }  
  80.       
  81.     int res = sqlite3_step(stmt2);  
  82.     CCLOG("res  %d",res);  
  83.     int fieldCount = sqlite3_column_count(stmt2);  
  84.     CCLOG("sqlite3_column_count(stmt2) fieldCount :%d",fieldCount);  
  85.     if (res ==SQLITE_ROW ) {  
  86.         for (int count = 0; count < fieldCount; count ++) {  
  87.             int stype = sqlite3_column_type(stmt2, count);  
  88.             if (stype == SQLITE_INTEGER) {  
  89.                 int temp1 = sqlite3_column_int(stmt2, count);  
  90.                 CCLOG("temp1  %d",temp1);  
  91.             }  
  92.             if (stype == SQLITE_FLOAT) {  
  93.                 double temp2 = sqlite3_column_double(stmt2, count);  
  94.                 CCLOG("temp2  %.2f",temp2);  
  95.             }  
  96.             if (stype == SQLITE_TEXT) {  
  97.                 const char *temp3 = (const char*)sqlite3_column_text(stmt2, count);  
  98.                 CCLOG("temp3  %s",temp3);  
  99.             }  
  100.             if (stype == SQLITE_NULL) {  
  101.                 CCLOG("NULL");  
  102.             }  
  103.               
  104.               
  105.         }  
  106.     }else if (res == SQLITE_DONE)  
  107.     {  
  108.         CCLOG("查询已经完成");  
  109.     }  
  110.     //释放创建表语句的对象内存  
  111.     sqlite3_finalize(stmt2);  
  112.       
  113.     //5.使用drop table 模块  
  114.     const char *deleteDatabase = "drop table testTable";  
  115.     sqlite3_stmt *stmt3;  
  116.     if (sqlite3_prepare_v2(testSqlite3, deleteDatabase, strlen(deleteDatabase), &stmt3, NULL)!= SQLITE_OK) {  
  117.         if (stmt3) {  
  118.             sqlite3_finalize(stmt3);  
  119.         }  
  120.         sqlite3_close(testSqlite3);  
  121.         return;  
  122.     }  
  123.     if (sqlite3_step(stmt3)==SQLITE_DONE) {  
  124.         CCLOG("drop the table succeed");  
  125.     }  
  126.     sqlite3_finalize(stmt3);  
  127.     sqlite3_close(testSqlite3);  
  128.     CCLOG("sqlite3_step(stmt3) %d",sqlite3_step(stmt3));  
  129.     CCLOG("Hello Sqlite!");  
  130.       
  131. }  

看下输出结果:

[cpp] 
  1. Cocos2d: resultOK  0  
  2. Cocos2d: length 88  
  3. Cocos2d: 0 :准备执行语句成功  
  4. Cocos2d: create the database successed!  
  5. Cocos2d: 插入数据成功  
  6. Cocos2d: length2 24  
  7. Cocos2d: res  100  
  8. Cocos2d: sqlite3_column_count(stmt2) fieldCount :4  
  9. Cocos2d: temp1  100  
  10. Cocos2d: temp2  100.00  
  11. Cocos2d: temp3  这是一个测试  
  12. Cocos2d: temp3  我的名字叫kevin  
  13. Cocos2d: drop the table succeed  
  14. Cocos2d: sqlite3_step(stmt3) 21  
  15. Cocos2d: Hello Sqlite!  
原文地址:

转载地址:http://chsmi.baihongyu.com/

你可能感兴趣的文章
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>
【LKM】整合多个LKM为1个
查看>>
【Windows C++】调用powershell上传指定目录下所有文件
查看>>
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>
Hive数据倾斜
查看>>
TopK问题
查看>>
HQL排查数据倾斜
查看>>
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>
MapReduce Task数目划分
查看>>