
正在做的项目需要sqlite数据库存储数据。小巧 、高效和易 *** 作是sqlite的明显特点,无平台更是强大。开源且免费啊,亲。
好的,下面步入正题。看下xcode下的Cocos2d—X的数据存储如何使用。
看下sqlite表的数据返回,会带有字段的一行:
As an example of the result table format,suppose a query result is as follows:
name | Age-----------------------Alice | 43Bob | 28Cindy | 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.
[cpp] view plain copy stringpath=CCfileUtils::sharedfileUtils()->getWritablePath()+"MysqLite.db"; remove(path.c_str()); intflag=access(path.c_str(),0); if(flag!=0){ userData.init(); }
copy voIDHelloWorld::init() { //1.创建数据库testsqlite3 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); //2.使用SQL语句创建表testtable,并设置字段 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!"); }
copy 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.jb51.cc/article/p-vsqehcsj-be.html 总结
以上是内存溢出为你收集整理的Cocos2d-X之游戏存储Sqlite基础篇(四)全部内容,希望文章能够帮你解决Cocos2d-X之游戏存储Sqlite基础篇(四)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)