Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2dx-sqlite3大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文出自 “老G的小屋” 博客,请务必保留此出处http://goldlion.blog.51cto.com/4127613/772518

一、安装与配置
sqlite是使用非常广泛的嵌入式数据库,它有着0配置,占用资源少等特点。从大型游戏《魔兽世界》到android上的很多游戏和软件(google提供了一个java语言的绑定。)

在cocos2d-x中,我们使用它的C语言绑定。
为了方便和简化篇幅,我们直接使用它的源代码。下载地址:
http://www.sqlite.org/sqlite-amalgamation-3071000.zip
将其解压到cocos2d-x引擎目录下,得到一个“sqlite-amalgamation-3071000”文件夹,里面有四个源文件。

在VC中新建一个项目,起名叫Save。

然后,右键点项目-》属性-》配置属性-》C++-》常规-》附加包含目录
添加刚才的解压的源代码路径。

下一步,右键点项目-》添加-》现有项,选择那四个源代码文件。然后sqlite就配置好了。


二、初步使用

HelloworldScene中,添加

1 #include "sqlite3.h"

然后在init函数中编写代码

01
sqlite3 *pDB = NULL;//数据库指针
02
char@H_450_116@* errMsg = NULL;//错误信息
03 std::String sqlstr;//SQL指令
04 int@H_450_116@result;//sqlite3_exec返回值
05
06 //打开一个数据库,如果该数据库不存在,则创建一个数据库文件
07 result = sqlite3_open("save.db",&pDB);
08 if( result != @R_598_5607@OK )
09 CCLog(@H_450_116@"打开数据库失败,错误码:%d错误原因:%s\n"@H_450_116@:1.1em!important; font-family:Consolas,result,errMsg );
10
11 //创建表,设置ID为主键,且自动增加
12 result=sqlite3_exec( pDB,@H_450_116@"create table myTable_1( ID INTEGER PRIMary key autoincrement,name nvarchar(32) ) "@H_450_116@:1.1em!important; font-family:Consolas,NULL,&errMsg );
13 ( result != @R_598_5607@OK )
14 "创建表失败,错误码:%d错误原因:%s\n"@H_450_116@:1.1em!important; font-family:Consolas,errMsg );
15 16 //插入数
17 sqlstr=" insert into myTable_1( name ) values ( '克塞' ) ";
18 result = sqlite3_exec( pDB,sqlstr.c_str(),monospace!important; font-weight:normal!important; font-style:normal!important; font-size:10pt!important; min-height:auto!important; display:block!important; BACkground:none!important">19 (result != @R_598_5607@OK )
20 "插入记录失败,错误码:%d错误原因:%s\n"@H_450_116@21 22 23 " insert into myTable_1( name ) values ( '葫芦娃' ) "24 25 26 27 28 29 " insert into myTable_1( name ) values ( '擎天柱' ) "30 31 32 33 34 //关闭数据库
35 sqlite3_close(pDB);


然后执行项目,你看不到什么东西,因为只是操作了数据库。

三、sqlite数据库管理工具

sqlite Database Browser是一个用Qt编写的跨平台sqlite数据库管理工具。这个工具的特点是非常简单易用, 甚至很多人拿这个修改sqlite游戏存档。(哈哈哈,关于sqlite加密问题,我们以后会讲。)
这里附上他的下载地址:
http://sourceforge.net/projects/sqlitebrowser/files/latest/download?source=files

然后,我们用这个工具,打开项目目录中resources目录下的save.db,就可以看到刚才生成的数据库数据了。

一共三个标签页,DataBase Structure、Browse Data,Execute SQL……意思一目了然,不用多说。是不是很好用啊,哈哈哈。


四、其他常见sqlite操作举例

还是以上面的表举例,直接给出操作代码。具体接口解释可以参官方文档:http://www.sqlite.org/docs.html
为了突出主要内容,删掉了一些调试信息。

1)更新记录
把第三条改成威震天
"update myTable_1 set name='威震天' where ID = 3"2 sqlite3_exec( pDB,&errMsg );

2)删除记录
把第二条葫芦娃删了
"delete from myTable_1 where ID = 2":1.1em!important; font-family:Consolas,&errMsg );

3)判断表是否存在
判断表myTable_1是否存在,保存在isExisted_中。
bool@H_450_116@isExisted_;
"SELEct count(typE) from sqlite_master where type='table' and name='myTable_1'";
3 :rgb(108,isExisted,&isExisted_,&errMsg );

这里用到了一个回调函数isExisted,他的定义如下:
isExisted(@H_450_116@void@H_450_116@* para,@H_450_116@n_column,0)!important; BACkground:none!important">** column_value,0)!important; BACkground:none!important">** column_name )
{
*isExisted_=(bool*)para;
4 *isExisted_=(**column_value)!='0'5 return@H_450_116@0;
6 }

4)判断记录是否存在
判断ID=2的记录是否存在,保存在isExisted_中。
"SELEct count(*) from myTable_1 where ID = 2":1.1em!important; font-family:Consolas,&errMsg );
回调函数isExisted的定义,在3)已给出,不再赘述。

5)获得记录条数
获得表myTable_1的记录条数,保存在count中。
count;
"SELEct * from myTable_1":1.1em!important; font-family:Consolas,loadRecordCount,&count,&errMsg );

这里用到了一个回调函数loadRecordCount,他的定义如下:
loadRecordCount(@H_450_116@*count=(int*count=n_column;
}

6)读取一条记录
读取表myTable_1中ID=3的记录,并打印
"SELEct * from myTable_1 where ID=3":1.1em!important; font-family:Consolas,loadRecord,&errMsg );

这里用到了一个回调函数loadRecord,他的定义如下:
loadRecord(@H_450_116@CCLog("ID=%s,name=%s":1.1em!important; font-family:Consolas,column_value[0],column_value[1]);
0;
}

大佬总结

以上是大佬教程为你收集整理的Cocos2dx-sqlite3全部内容,希望文章能够帮你解决Cocos2dx-sqlite3所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:3cocoscocos2dxdxsqlitesqlite3
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap