正在做的项目需要Sqlite数据库存储数据。小巧 、高效和易操作是Sqlite的明显特点,无平台更是强大。开源且免费啊,亲。
好的,下面步入正题。看下xcode下的Cocos2d—X的数据存储如何使用。
看下sqlite表的数据返回,会带有字段的一行:
As an example of the result table format,suppose a query result is as follows:
Name | Age
-----------------------
Alice | 43
Bob | 28
Cindy | 21
There are two column (M==2) and three rows (N==3). Thus the result table has 8 entries. Suppose the result table is stored in an array names azResult. Then azResult holds this content:
azResult[0] = "Name";
azResult[1] = "Age";
azResult[2] = "Alice";
azResult[3] = "43";
azResult[4] = "Bob";
azResult[5] = "28";
azResult[6] = "Cindy";
azResult[7] = "21";
在helloWorld的初始化直接写了。废话不说,直接代码贴出:
access函数判断文件存在与否,mode为0.
- stringpath=CCFileUtils::sharedFileUtils()->getWritablePath()+"mySqlite.db";
-
- remove(path.c_str());
- intflag=access(path.c_str(),0);
- if(flag!=0){
- userData.init();
- }
#include<sqlite3.h>
voidHelloWorld::init()
- {
-
- sqlite3*testSqlite3=NULL;
- //sqlite3_open()方法会打开数据库,没有就自动创建一个
- intresultOK=sqlite3_open("/Users/kevin/Desktop/testSqlite9.db",&testSqlite3);
- //返回数字0,说明创建成功
- if(resultOK!=SQLITE_OK){
- sqlite3_close(testSqlite3);
- return;
- }
- CCLOG("resultOK%d",resultOK);
-
-
- constchar*createTableSQL="createtabletestTable(int_colINTERGER,float_colREAL,string_colTEXT,name_colTEXT)";
- //stmt,即是statement句柄,承载着sql语句
- sqlite3_stmt*stmt=NULL;
- //sql语句的长度
- intlength=strlen(createTableSQL);
- CCLOG("length%d",length);
- //sqlite3_prepare_v2,准备创建数据表,参数分别是数据库名称,表名称,语句长度,句柄。如果写入错误,则释放stmt对象,关闭数据库
- if(sqlite3_prepare_v2(testSqlite3,createTableSQL,length,&stmt,NULL)!=SQLITE_OK){
- if(stmt){
- sqlite3_finalize(stmt);
- sqlite3_close(testSqlite3);
- return;
- }
- CCLOG("%d:准备执行语句成功",sqlite3_prepare_v2(testSqlite3,NULL));
- //sqlite3_step执行创建语句,如果创建错误,则释放stmt对象,关闭数据库
- if(sqlite3_step(stmt)!=SQLITE_DONE){
- //CCLOG("sqlite3_step(stmt)%d",sqlite3_step(stmt));
- //释放创建表语句的对象内存
- CCLOG("createthetablesuccessed!");
- //3.insert表数据
- char*insertDatabase="insertintotestTablevalues(100,100,'这是一个测试','我的名字叫kevin')";
- sqlite3_stmt*stmt4;
- if(stmt4){
- sqlite3_finalize(stmt4);
- intres1=sqlite3_step(stmt4);
- if(res1!=SQLITE_DONE){
- sqlite3_finalize(stmt4);
- CCLOG("插入数据成功");
- //成功后清除句柄对象
- //4.使用sql查询语句
- char*selectSQL="select*fromtestTable";
- sqlite3_stmt*stmt2=NULL;
- intlength2=strlen(selectSQL);
- CCLOG("length2%d",length2);
- if(stmt2){
- sqlite3_finalize(stmt2);
- intres=sqlite3_step(stmt2);
- CCLOG("res%d",res);
- intfieldCount=sqlite3_column_count(stmt2);
- CCLOG("sqlite3_column_count(stmt2)fieldCount:%d",fieldCount);
- if(res==SQLITE_ROW){
- for(intcount=0;count<fieldCount;count++){
- intstype=sqlite3_column_type(stmt2,count);
- if(stype==SQLITE_INTEGER){
- inttemp1=sqlite3_column_int(stmt2,248)"> CCLOG("temp1%d",temp1);
- if(stype==SQLITE_FLOAT){
- doubletemp2=sqlite3_column_double(stmt2,248)"> CCLOG("temp2%.2f",temp2);
- if(stype==SQLITE_TEXT){
- char*temp3=(char*)sqlite3_column_text(stmt2,248)"> CCLOG("temp3%s",temp3);
- if(stype==SQLITE_NULL){
- CCLOG("NULL");
- }elseif(res==SQLITE_DONE)
- {
- CCLOG("查询已经完成");
- //5.使用droptable模块
- char*deleteDatabase="droptabletestTable";
- sqlite3_stmt*stmt3;
- if(stmt3){
- sqlite3_finalize(stmt3);
- if(sqlite3_step(stmt3)==SQLITE_DONE){
- CCLOG("dropthetablesucceed");
- CCLOG("sqlite3_step(stmt3)%d",sqlite3_step(stmt3));
- CCLOG("HelloSqlite!");
- }
看下输出结果:
Cocos2d:resultOK0
- Cocos2d:length88
- Cocos2d:0:准备执行语句成功
- Cocos2d:createthedatabasesuccessed!
- Cocos2d:插入数据成功
- Cocos2d:length224
- Cocos2d:res100
- Cocos2d:sqlite3_column_count(stmt2)fieldCount:4
- Cocos2d:temp1100
- Cocos2d:temp2100.00
- Cocos2d:temp3这是一个测试
- Cocos2d:temp3我的名字叫kevin
- Cocos2d:dropthetablesucceed
- Cocos2d:sqlite3_step(stmt3)21
- Cocos2d:HelloSqlite!
原文地址:http://www.52php.cn/article/p-vsqehcsj-be.html
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|