加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite专题

发布时间:2020-12-12 20:03:13 所属栏目:百科 来源:网络整理
导读:原创文章,转载请注明出处:http://www.jb51.cc/article/p-dfllfnds-o.html。 大家好 , 很长一段时间没有跟新我的博客 , 看到大家在我的博客里的留言,我很是欣慰。这次我给大家带来了cocos2d-x使用SQLite数据库的博文。 在cocos2d-x中,简单数据存储,可

原创文章,转载请注明出处:http://www.52php.cn/article/p-dfllfnds-o.html。


大家好很长一段时间没有跟新我的博客看到大家在我的博客里的留言,我很是欣慰。这次我给大家带来了cocos2d-x使用SQLite数据库的博文。


在cocos2d-x中,简单数据存储,可以使用CCUserDefault,大家可以看看我前一篇博文。至于大量,不规则的数据存储,则使用SQLite数据库。SQLite数据库的使用,请参考我的博文C++操作SQLite数据库,在此博文中,主要讲解怎么样使用C++来操作数据库的。SQLite数据库本来就是使用C语言来编写的,所以cocos2d-x使用SQLite也是得心应手。


其实cocos2d-x操作数据库在windows上的使用我早就解决了,主要是在android平台上的使用了。


先来看看代码怎么写,代码就是王道。


首先是要去SQLite官网下载SQLite的源代码。就是两个文件sqlite3.h和sqlite3.c,我在博文的最后会提供下载的。将这两个文件加入工程。

[cpp] view plain copy
  1. sqlite3*pdb=NULL;
  2. std::stringpath=CCFileUtils::sharedFileUtils()->getWriteablePath()+"save.db3";
  3. std::stringsql;
  4. intresult;
  5. result=sqlite3_open(path.c_str(),&pdb);
  6. if(result!=SQLITE_OK)
  7. CCLog("opendatabasefailed,number%d",result);

上面的代码就是创建SQLite数据库文件,注意:一定要提供文件的路径。

copy
    result=sqlite3_exec(pdb,"createtablestudent(IDintegerprimarykeyautoincrement,nametext,sextext)",NULL,NULL);
  1. CCLog("createtablefailed");

这是创建表的SQL语句。

copy
    sql="insertintostudentvalues(1,'zhycheng','male')";
  1. result=sqlite3_exec(pdb,sql.c_str(),NULL);
  2. if(result!=SQLITE_OK)
  3. CCLog("insertdatafailed!");
  4. sql="insertintostudentvalues(2,'liuyali','female')";
  5. result=sqlite3_exec(pdb,NULL);
  6. CCLog("insertdatafailed!");
  7. sql="insertintostudentvalues(3,'zhy_cheng',248); line-height:18px"> CCLog("insertdatafailed!");
插入数据。


copy
    sql="deletefromstudentwhereID=1";
  1. CCLog("deletedatafailed!");
删除数据。

copy
    char**re;
  1. intr,c;
  2. sqlite3_get_table(pdb,"select*fromstudent",&re,&r,&c,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> CCLog("rowis%d,columnis%d",r,c);
  3. CCLabelTTF*liu=CCLabelTTF::create(re[2*c+1],"Arial",24);
  4. liu->setPosition(ccp(200,200));
  5. addChild(liu,1);
  6. CCLog(re[2*c+1]);
  7. sqlite3_free_table(re);
  8. sqlite3_close(pdb);

查询数据。

这里数据查出后,使用一个文本在屏幕上显示,这是为了在Android手机上看到效果。


最后要注意,在发布到Android手机上的时候一定要修改MK文件,该文件是proj.android/jin/Android.mk。添加对sqlite3.c的编译,改成如下:

copy
    LOCAL_PATH:=$(callmy-dir)
  1. include$(CLEAR_VARS)
  2. LOCAL_MODULE:=game_shared
  3. LOCAL_MODULE_FILENAME:=libgame
  4. LOCAL_SRC_FILES:=hellocpp/main.cpp
  5. ../../Classes/AppDelegate.cpp
  6. ../../Classes/HelloWorldScene.cpp
  7. ../../Classes/sqlite3.c
  8. LOCAL_C_INCLUDES:=$(LOCAL_PATH)/../../Classes
  9. LOCAL_WHOLE_STATIC_LIBRARIES:=cocos2dx_staticcocosdenshion_staticcocos_extension_static
  10. include$(BUILD_SHARED_LIBRARY)
  11. $(callimport-module,CocosDenshion/android)
  12. $(callimport-module,cocos2dx)
  13. 好了,在我的手机上的运行效果如下:



    数据库文件保存的位置是/data/data/com.zhycheng.SQLiteTest/save.db3。


    最后提供Android的源代码下载:点击打开链接。

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读