cocos2d-x SQLite基本使用
2012-04-28 10:27
267 查看
一、安装与配置
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中,添加
然后在init函数中编写代码
然后执行项目,你看不到什么东西,因为只是操作了数据库。
三、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)更新记录
把第三条改成威震天
2)删除记录
把第二条葫芦娃删了
3)判断表是否存在
判断表MyTable_1是否存在,保存在isExisted_中。
这里用到了一个回调函数isExisted,他的定义如下:
4)判断记录是否存在
判断ID=2的记录是否存在,保存在isExisted_中。
回调函数isExisted的定义,在3)已给出,不再赘述。
5)获得记录条数
获得表MyTable_1的记录条数,保存在count中。
这里用到了一个回调函数loadRecordCount,他的定义如下:
6)读取一条记录
读取表MyTable_1中ID=3的记录,并打印
这里用到了一个回调函数loadRecord,他的定义如下:
转载 “老G的小屋”
博客
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中,添加
#include "sqlite3.h"
然后在init函数中编写代码
sqlite3 *pDB = NULL;//数据库指针 char * errMsg = NULL;//错误信息 std::string sqlstr;//SQL指令 int result;//sqlite3_exec返回值 //打开一个数据库,如果该数据库不存在,则创建一个数据库文件 result = sqlite3_open("save.db", &pDB); if( result != SQLITE_OK ) CCLog( "打开数据库失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); //创建表,设置ID为主键,且自动增加 result=sqlite3_exec( pDB, "create table MyTable_1( ID integer primary key autoincrement, name nvarchar(32) ) " , NULL, NULL, &errMsg ); if( result != SQLITE_OK ) CCLog( "创建表失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); //插入数据 sqlstr=" insert into MyTable_1( name ) values ( '克塞' ) "; result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); if(result != SQLITE_OK ) CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); //插入数据 sqlstr=" insert into MyTable_1( name ) values ( '葫芦娃' ) "; result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); if(result != SQLITE_OK ) CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); //插入数据 sqlstr=" insert into MyTable_1( name ) values ( '擎天柱' ) "; result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg ); if(result != SQLITE_OK ) CCLog( "插入记录失败,错误码:%d ,错误原因:%s\n" , result, errMsg ); //关闭数据库 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)更新记录
把第三条改成威震天
qlstr="update MyTable_1 set name='威震天' where ID = 3"; sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
2)删除记录
把第二条葫芦娃删了
sqlstr="delete from MyTable_1 where ID = 2"; sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
3)判断表是否存在
判断表MyTable_1是否存在,保存在isExisted_中。
bool isExisted_; sqlstr="select count(type) from sqlite_master where type='table' and name='MyTable_1'"; sqlite3_exec( pDB, sqlstr.c_str() , isExisted, &isExisted_, &errMsg );
这里用到了一个回调函数isExisted,他的定义如下:
int isExisted( void * para, int n_column, char ** column_value, char ** column_name ) { bool *isExisted_=(bool*)para; *isExisted_=(**column_value)!='0'; return 0; }
4)判断记录是否存在
判断ID=2的记录是否存在,保存在isExisted_中。
bool isExisted_; sqlstr="select count(*) from MyTable_1 where ID = 2"; sqlite3_exec( pDB, sqlstr.c_str() , isExisted, &isExisted_, &errMsg );
回调函数isExisted的定义,在3)已给出,不再赘述。
5)获得记录条数
获得表MyTable_1的记录条数,保存在count中。
int count; sqlstr="select * from MyTable_1"; sqlite3_exec( pDB, sqlstr.c_str() , loadRecordCount, &count, &errMsg );
这里用到了一个回调函数loadRecordCount,他的定义如下:
int loadRecordCount( void * para, int n_column, char ** column_value, char ** column_name ) { int *count=(int*)para; *count=n_column; return 0; }
6)读取一条记录
读取表MyTable_1中ID=3的记录,并打印
sqlstr="select * from MyTable_1 where ID=3"; sqlite3_exec( pDB, sqlstr.c_str() , loadRecord, NULL, &errMsg );
这里用到了一个回调函数loadRecord,他的定义如下:
int loadRecord( void * para, int n_column, char ** column_value, char ** column_name ) { CCLog("ID=%s,name=%s",column_value[0],column_value[1]); return 0; }
转载 “老G的小屋”
博客
相关文章推荐
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用 推荐
- 【Cocos2d-x 020】 sqlite基本sql语句使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x学习笔记17:记录存储2:SQLite基本使用
- cocos2d-x中SQLite基本使用
- 黑马程序员_Android_SQLite基本概念与使用
- SQLite的基本信息和使用过程
- iOS sqlite3 的基本使用(增 删 改 查)
- android数据库sqlite的基本使用
- cocos2d-x 下使用加密 sqlite3
- SQLite 基本语句使用
- Python SQLite基本使用
- SQLite的基本使用介绍【教程5--图解】
- sqlite基本sql语句使用
- sqlite基本sql语句使用
- Android中SQLite的基本使用(二)
- sqlite的基本使用示例
- sqlite基本语句使用
- Cocos2d-x 3.9教程: 2.菜单项的基本使用