SQLite集成与用法

SQLite集成与用法

概述

在Cocos2d-x中,简单数据存储,可以使用UserDefault。那么如何存储大量,不规则的数据?我们可以使用SQLite数据库存储数据。SQLite是使用非常广泛的嵌入式数据库,它有小巧 、高效、跨平台、开源免费和易操作的特点。

SQLite数据库是使用C语言来编写的,那么在Cocos2d-x使用SQLite也是得心应手。

准备

首先创建一个Cocos2d-x v3.x的helloworld工程,我们将以该工程作为SQLite集成与用法的实战工程。

打开终端,使用如下命令新建工程:

1
cocos new HelloWorld -p com.your_company.HelloWorld -l cpp

按照上面的操作,我们新建了一个Cocos2d-x v3.x的HelloWorld工程。

iOS/Mac

iOS/Mac的系统库自带sqlite库,我们只需添加libsqlite3.0.dylib库即可。

Android

Android系统没有自带sqlite库,我们需要手动添加。

1.下载sqlite包

下载地址:http://www.sqlite.org/download.html 下载后,在项目中导入sqlite3.c和sqlite3.h两个文件即可。

2.导入到工程

3.修改Android.mk

使用SQLite

打开HelloWorldScene.cpp文件,我们在里面加入SQLite的使用示例

引入头文件

1
#include "sqlite3.h"

创建SQLite 数据库

1
2
3
4
5
6
7
8
9
10
sqlite3 *pdb=NULL; //1
std::string path= FileUtils::getInstance()->getWritablePath()+ "save.db" ; //2
 
std::string sql;
int result;
result=sqlite3_open(path.c_str(),&pdb); //3
if (result!=SQLITE_OK)
{
     log ( "open database failed,  number%d" ,result);
}
  1. 数据库指针
  2. 指定数据库的路径
  3. 打开一个数据库,如果该数据库不存在,则创建一个数据库文件

SQL语句

1
sql= "create table student(ID integer primary key autoincrement,name text,sex text)" ; //1
  1. 创建表的SQL语句

创建Table

1
2
3
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL); //1
if (result!=SQLITE_OK)
     log ( "create table failed" );
  1. 创建表

插入数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sql= "insert into student  values(1,'student1','male')" ;
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if (result!=SQLITE_OK)
     log ( "insert data failed!" );
 
sql= "insert into student  values(2,'student2','female')" ;
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if (result!=SQLITE_OK)
     log ( "insert data failed!" );
 
sql= "insert into student  values(3,'student3','male')" ;
result=sqlite3_exec(pdb,sql.c_str(),NULL,NULL,NULL);
if (result!=SQLITE_OK)
     log ( "insert data failed!" );
  1. 向表中插入3条数据

查询

1
2
3
4
5
6
7
8
9
10
11
12
13
char **re; //查询结果
int r,c; //行、列
sqlite3_get_table(pdb, "select * from student" ,&re,&r,&c,NULL); //1
log ( "row is %d,column is %d" ,r,c);
 
for ( int i=1;i<=r;i++) //2
{
     for ( int j=0;j<c;j++)
     {
         log ( "%s" ,re[i*c+j]);
     }
}
sqlite3_free_table(re);
  1. 查询表中的数据
  2. 将查询结果的log输出

查询结果:

1
2
3
4
5
6
7
8
9
10
cocos2d: row is 3,column is 3
cocos2d: 1
cocos2d: student1
cocos2d: male
cocos2d: 2
cocos2d: student2
cocos2d: female
cocos2d: 3
cocos2d: student3
cocos2d: male

我们可以看到查询到结果和我们前面插入的数据一样。

删除

1
2
3
4
sql= "delete from student where ID=1" ;
result=sqlite3_exec(pdb,sql.c_str(), NULL,NULL,NULL); //1
if (result!=SQLITE_OK)
     log ( "delete data failed!" );
  1. 删除ID=1的学生

使用上面的查询语句查询删除ID=1的学生后的数据

查询结果:

1
2
3
4
5
6
7
cocos2d: row is 2,column is 3
cocos2d: 2
cocos2d: student2
cocos2d: female
cocos2d: 3
cocos2d: student3
cocos2d: male

我们可以看到,表中ID=1的数据已经被删除了

注意:

使用sqlite一定要注意的内存管理问题,那就是打开数据库之后,数据操作完成之后,一定要关闭数据库,否侧会造成内存泄漏。

1
sqlite3_close(pdb);

数据文件存放的位置

  • Android:
1
/data/data/com.youCompany.Helloworld/files/save.db
  • iOS:

位于程序沙盒的文档目录下

1
../Documents/save.db


    sqlite3* pdb = NULL;
    std::string path = FileUtils::getInstance()->getWritablePath() + "test.db";
    std::string sql;
    int result;
    result = sqlite3_open(path.c_str(), &pdb);
    if(result != SQLITE_OK)
    {
        log("open database failed, number%d",result);
    }
    
    sql = "create table student(ID integer primary key autoincrement, name text,sex text)";
    result = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
    if(result != SQLITE_OK)
    {
        log("create table failed");
    }
    
    sql = "insert into student values(1,'student1','yuxikuo')";
    result = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
    if(result != SQLITE_OK)
    {
        log("insert data failde");
    }
    
    sql = "insert into student values(2,'student2','wangdongge')";
    result = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
    if(result != SQLITE_OK)
    {
        log("insert data failde");
    }

    sql = "insert into student values(3,'student3','dujia')";
    result = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
    if(result != SQLITE_OK)
    {
        log("insert data failde");
    }
    
    char **p;
    int r,c;
    sqlite3_get_table(pdb, "select * from student", &p, &r, &c, NULL);
    log("row is %d,column is %d",r,c);
    for (int i = 1; i <= r ; i++)
    {
        for(int j = 0; j < c; j++)
        {
            log("%s",p[i*c+j]);
        }
    }
    sqlite3_free_table(p);
    
    sql = "delete from student where ID=1";
    result = sqlite3_exec(pdb, sql.c_str(), NULL, NULL, NULL);
    if(result != SQLITE_OK)
    {
        log("delete data failed");
    }
    
    sqlite3_get_table(pdb, "select * from student", &p, &r, &c, NULL);
    log("row is %d,column is %d",r,c);
    for (int i = 1; i <= r ; i++)
    {
        for(int j = 0; j < c; j++)
        {
            log("%s",p[i*c+j]);
        }
    }

    sqlite3_close(pdb);




转载自:http://cn.cocos2d-x.org/article/index?type=cocos2d-x&url=/doc/cocos-docs-master/manual/framework/native/v3/sqlite/zh.md
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值