SQLite的基本使用

来源:互联网 发布:mac os 常用软件 编辑:程序博客网 时间:2024/04/30 15:06
在HelloworldScene中,添加

1#include "sqlite3.h"

然后在init函数中编写代码

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


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


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

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


四、其他常见SQLite操作举例

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

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

2)删除记录
把第二条葫芦娃删了 
1sqlstr="delete from MyTable_1 where ID = 2";
2 sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );

3)判断表是否存在
判断表MyTable_1是否存在,保存在isExisted_中。
1bool isExisted_;
2sqlstr="select count(type) from sqlite_master where type='table' and name='MyTable_1'";
3sqlite3_exec( pDB, sqlstr.c_str() , isExisted, &isExisted_, &errMsg );

这里用到了一个回调函数isExisted,他的定义如下:
1int isExisted( void * para, int n_column, char ** column_value, char ** column_name )
2{
3            bool *isExisted_=(bool*)para;
4            *isExisted_=(**column_value)!='0';
5            return 0;
6}

4)判断记录是否存在
判断ID=2的记录是否存在,保存在isExisted_中。
1bool isExisted_;
2sqlstr="select count(*) from MyTable_1 where ID = 2";
3sqlite3_exec( pDB, sqlstr.c_str() , isExisted, &isExisted_, &errMsg );
回调函数isExisted的定义,在3)已给出,不再赘述。

5)获得记录条数
获得表MyTable_1的记录条数,保存在count中。 
1int count;
2 sqlstr="select * from MyTable_1";
3 sqlite3_exec( pDB, sqlstr.c_str() , loadRecordCount, &count, &errMsg );

这里用到了一个回调函数loadRecordCount,他的定义如下:
1int loadRecordCount( void * para, int n_column, char ** column_value, char ** column_name )
2{
3            int *count=(int*)para;
4            *count=n_column;
5            return 0;
6}

6)读取一条记录
读取表MyTable_1中ID=3的记录,并打印
1sqlstr="select * from MyTable_1 where ID=3";
2sqlite3_exec( pDB, sqlstr.c_str() , loadRecord, NULL, &errMsg );

这里用到了一个回调函数loadRecord,他的定义如下:
view source
print?
1int loadRecord( void * para, int n_column, char ** column_value, char ** column_name )
2{
3            CCLog("ID=%s,name=%s",column_value[0],column_value[1]);
4            return 0;
5}

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


0 0
原创粉丝点击