[置顶] cocos2dx sqllite 增删查改等操作
首先导入文件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 增删查改等操作的更多相关文章
- SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)
SQL Server 表的管理_关于数据增删查改的操作的详解(案例代码)-DML 1.SQL INSERT INTO 语句(在表中插入) INSERT INTO 语句用于向表中插入新记录. SQL I ...
- SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)
SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码) 概述: 表由行和列组成,每个表都必须有个表名. SQL CREATE TABLE 语法 CREATE TABLE tabl ...
- php学习笔记:对文件的增删查改等操作
文件的创建: 采用touch()函数,当文件不存在会被创建 例如: <?php header("Content-type: text/html; charset=utf-8" ...
- Elasticsearch(ES)API 增删查改常用操作
常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...
- 4.在MVC中使用仓储模式进行增删查改
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...
- 一套手写ajax加一般处理程序的增删查改
倾述下感受:8天16次驳回.这个惨不忍睹. 好了不说了,说多了都是泪. 直接上代码 : 这个里面的字段我是用动软生成的,感觉自己手写哪些字段太浪费时间了,说多了都是泪 ajax.model层的代码: ...
- 步步为营-42-通过DataAdapter实现增删查改
说明:通过DataAdapter或者dataset连接数据库,实现对数据增删改查操作. 以前写过一篇步步为营-23-通过GridView实现增删改 1:SqlDataAdapter DataTabl ...
- MongoDB入门学习(三):MongoDB的增删查改
对于我们这样的菜鸟来说,最重要的不是数据库的管理,也不是数据库的性能,更不是数据库的扩展,而是怎么用好这款数据库,也就是一个数据库提供的最核心的功能,增删查改. 由于M ...
- SpringBoot整合Mybatis-plus实现增删查改
今天给大家分享一下SpringBoot整合Mybatis-plus的增删查改案例. pom.xml <?xml version="1.0" encoding="UT ...
随机推荐
- 更新ACCESS数据库出现“字段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据。”的解决方法
今天进行数据调试时出现“字段太小而不能接受所要添加的数据的数量.试着插入或粘贴较少的数据.”,跟踪发现是在更新数据库的数据时出现的. 打开数据库表格发现出错的数据字段类型被定义为“文本”,也就是数据最 ...
- JS选择checkbox
<script> window.onload = function () { //获取checkbox var ids1 = document.getElementsByName('Vot ...
- mysqli 扩展库的预处理技术(mysqli_stmt)
提出问题 现在需要向mysql数据库中添加100个用户,请问如何实现? 方法一:for循环100次 方法二:使用批量添加 $sqls="insert xxx"; $sqls.=&q ...
- 自己编写的sublime text 3 插件
一些小功能,比较杂. 具体的功能在这里查看 1.本地环境的php运行结果获取. 2.快捷打开常用的文件,文件夹,url.(ctrl+shift+a) 3.常用的缩进转换. 下边是网络爬虫代码. #py ...
- 面试题: generate an equation, by inserting operator add ("+") and minus ("-") among the array to make equationExpression == 0
package com.Amazon.interview; /** * @Author: weblee * @Email: likaiweb@163.com * @Blog: http://www.c ...
- 4070: [Apio2015]雅加达的摩天楼
Description 印尼首都雅加达市有 N 座摩天楼,它们排列成一条直线,我们从左到右依次将它们编号为 0 到 N−1.除了这 N 座摩天楼外,雅加达市没有其他摩天楼. 有 M 只叫做 “do ...
- ASP.NET State Service服务
ASP.NET State Service服务是用来管理 Session 的,正常来说,Session 位于IIS进程中(其实可以理解成在服务器的内存中),当IIS重启或程序池回收会自动清空Sessi ...
- delphi xe5 android 服务端和手机端的源码下载
xe5 android的服务端和手机客户端的源代码下载地址 http://files.cnblogs.com/nywh2008/AndroidTest.rar
- EasyUI datagrid数据表格的函数getData返回来的是什么
EasyUI datagrid数据表格的函数getData返回来的是什么? 他返回来的是这么一个对象: Object { rows=[10], total=15} 其中rows就是每一行的数据,是这些 ...
- 【POJ 3167】Cow Patterns (KMP+树状数组)
Cow Patterns Description A particular subgroup of K (1 <= K <= 25,000) of Farmer John's cows l ...