首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会出错,当时搞了很久发现的)

创建数据库

	sqlite3 *pDB = NULL;			//数据库指针
char * errMsg = NULL; //错误信息
std::string sqlstr; //SQL指令
int result; //sqlite3_exec返回值
bool isExisted_; //判断是否开始
char db[100]="";
std::string path = CCFileUtils::sharedFileUtils()->getWritablePath();
strcpy(db, path.c_str());
strcat(db,"save.db"); result = sqlite3_open(db, &pDB);//如果不存在及新建数据库,存在及打开 sqlstr="select count(type) from sqlite_master where type='table' and name='achievement_t'";//是否存在这个表
sqlite3_exec( pDB, sqlstr.c_str() , ::isExisted, &isExisted_, &errMsg );//不存在 及isExisted_为false

函数isExisted

int isExisted( void * para, int n_column, char ** column_value, char ** column_name )
{
bool *isExisted_=(bool*)para;
*isExisted_=(**column_value)!='0';
return 0;
}

创建表

result=sqlite3_exec( pDB, "create table achievement_t( id integer primary key autoincrement, stute integer ) " , NULL, NULL, &errMsg );

插入数据

		sqlstr=" insert into achievement_t( stute) values ( 0 ) ";
for(int i=0;i<15;i++){
result = sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
}

以上是我做项目的

对于那些增删查改如下例子

	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 );
//查询语句
sqlstr="select * from MyTable_1";
result= sqlite3_exec( pDB, sqlstr.c_str() ,loadRecord, 0, &errMsg );
//关闭数据库
sqlite3_close(pDB);

回到我自己项目需要批量查询我用

	sqlite3 *pDB = NULL;			//数据库指针
char * errMsg = NULL; //错误信息
std::string sqlstr; //SQL指令
char db[100]="";
std::string path = CCFileUtils::sharedFileUtils()->getWritablePath();
strcpy(db, path.c_str());
strcat(db,"save.db");
int* count=new int[15];
sqlite3_open(db, &pDB);
for(int i=0;i<15;i++){
char temp[80];
int* inttemp=new int[1];
sprintf(temp, "select stute from achievement_t where ID = %d",i+1);
sqlstr=temp;
sqlite3_exec( pDB, sqlstr.c_str() , DataControl::queryCallBack, inttemp, &errMsg );
*(count+i)=*inttemp;
}
sqlite3_close(pDB);
return count;

添加这个函数(这个我的理解也不够清楚 我就照着别人写的 大家指教一下哈)

int DataControl::queryCallBack(void* para,int n_column,char** column_value,char** column_name)
{
//para是你在sqlite3_exec 里传入的void*参数通过para参数,你可以传入一些特殊的指针(比如类指针、结构指针),
//然后在这里面强制转换成对应的类型(这里面是void*类型,必须强制转换成你的类型才可用)。然后操作这些数据
//n_column是这一条记录有多少个字段(即这条记录有多少列)
//char** column_value 是个关键值,查出来的数据都保存在这里,它实际上是个1维数组(不要以为是2维数组),
//每一个元素都是一个char*值,是一个字段内容(用字符串来表示,以\0结尾)
//char** column_name 跟column_value是对应的,表示这个字段的字段名称
int *temp = (int*)para;
int count=0;
sscanf(*(column_name+1),"%d",&count);
*temp=count;
return 0;
}

如插入批量数据

	sqlite3 *pDB = NULL;			//数据库指针
char * errMsg = NULL; //错误信息
char db[100]="";
std::string path = CCFileUtils::sharedFileUtils()->getWritablePath();
strcpy(db, path.c_str());
strcat(db,"save.db");
sqlite3_open(db, &pDB);
std::string sqlstr;//SQL指令
char temp[80];
for(int i=0;i<8;i++){
sprintf(temp, "update prop_t set rank=%d where ID = %d",prop[i],i+1);
sqlstr=temp;
sqlite3_exec( pDB, sqlstr.c_str() , NULL, NULL, &errMsg );
}
sqlite3_close(pDB);

可下载例子 :http://download.csdn.net/detail/five50/5663775

[置顶] cocos2dx sqllite 增删查改等操作的更多相关文章

  1. SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...

  2. SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)

    SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码) 概述: 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE tabl ...

  3. php学习笔记:对文件的增删查改等操作

    文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...

  4. Elasticsearch(ES)API 增删查改常用操作

    常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...

  5. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  6. 一套手写ajax加一般处理程序的增删查改

    倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...

  7. 步步为营-42-通过DataAdapter实现增删查改

    说明:通过DataAdapter或者dataset连接数据库,实现对数据增删改查操作. 以前写过一篇步步为营-23-通过GridView实现增删改 1:SqlDataAdapter  DataTabl ...

  8. MongoDB入门学习(三):MongoDB的增删查改

            对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改.         由于M ...

  9. SpringBoot整合Mybatis-plus实现增删查改

    今天给大家分享一下SpringBoot整合Mybatis-plus的增删查改案例. pom.xml <?xml version="1.0" encoding="UT ...

随机推荐

  1. 【转】EXT JS MVC开发模式

    原文链接:EXT JS MVC开发模式 在app(亦即根目录)文件夹下面创建controller.model.store和view文件夹,从名称上就知道他们该放置什么代码了吧.然后创建Applicat ...

  2. 学习PHP 301跳转的方法

    发布:JB01   来源:脚本学堂     [大 中 小]本文详细介绍了,在php编程中实现301跳转,即301永久重定向的方法,感兴趣的朋友可以参考学习下. 本文转自:http://www.jbxu ...

  3. 《工作型PPT设计之道》培训心得

    参加包翔老师的“工作型PPT设计之道>培训,颇多心得,后来为部门新员工和同组同事做了转化培训,将心得整理成一份PPT讲义,效果颇佳.现将主要心得整理于此.因时间仓促,24条心得有拼凑之嫌,有待今 ...

  4. 005 Python的数值类型

    005 Python的数值类型 BIF    指的是内置函数,一般不作为变量命名.如 input,while,if,else,float,等等.整型:整数.(python3.0版本把整型和长整型结合在 ...

  5. 转载:Source Insight查看ARM汇编源程序 && 高亮显示程序 && Source Insight打开project窗口出错

    (1)Source Insight查看ARM汇编源程序.做ARM嵌入式开发时,有时得整汇编代码,但在SIS里建立PROJECT并ADD TREE的时候,根据默认设置并不会把该TREE里面所有汇编文件都 ...

  6. wysiwyg editor

    http://www.bootcss.com/p/bootstrap-wysiwyg/

  7. nginx处理静态资源的配置

    修改nginx.conf文件,用于nginx处理静态资源. 主要配置如下(在server配置中加入location配置即可): server { listen 80; server_name 123. ...

  8. c/c++多级指针

    c/c++多级指针 如图: # include <stdio.h> int main(void) { ; int * p = &i; //p只能存放int类型变量的地址 int * ...

  9. bzoj 3527: [Zjoi2014]力 快速傅里叶变换

    题意: 给出n个数qi,给出Fj的定义如下:  令Ei=Fi/qi,求Ei. fft的那一堆东西还是背不到啊...这次写虽说完全自己写的,但是还是在参见了以前fft程序的情况下调了很久,主要在如下几点 ...

  10. IAR ARM、IAR STM8、IAR MSP430共用一个IDE

    转自IAR ARM.IAR STM8.IAR MSP430共用一个IDE 试了安装好多个不同版本不同编译器的IAR,终于明白不同编译器的IAR共用IDE的条件,把几个不同编译器的IAR安装在一起,共用 ...