使用cocos2d-x的数据库(例如sqlite) [英] Use database (such as sqlite) with cocos2d-x

查看:146
本文介绍了使用cocos2d-x的数据库(例如sqlite)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始在iphone上构建游戏应用程序。我正在使用cocos2d-x游戏引擎,因为它很容易从那里移植到android。编码也是用C ++编写的,我非常熟悉。我想知道是否有办法使用cocos2d-x的任何数据库。虽然sqlite是首选但不是强制性的。我将在数据库中有大约1/2 MB的数据。所以,是的,我也考虑过保留/使用内存数据库,但我希望我的读/写查询能够节省时间。

I am starting to build a gaming application on iphone. I am using cocos2d-x game engine, since it is easy to port to android from there. Also the coding is in C++, which I am very much familiar with. I want to know if there is a way to use any database with cocos2d-x. Although sqlite is preferred but not mandatory. I will have about 1/2 mb of data in the database. So, yes I have thought about keeping/using an in-memory database too, but I want my read/write queries to be time efficient.

我已经抬头看了一些博客,这表明我可能需要为sqlite使用C ++包装器。问题是,对于一个独立的C ++代码,我可以设置环境,但是我如何将它集成到xcode(在mac os中)以使用sqlite和cocos2d-x。

I have looked up at some of blogs, which suggest that I may need to use a C++ wrapper for sqlite. The problem is, for an independent C++ code, I can setup the environment, but how do I integrate this in xcode (in mac os) to use sqlite with cocos2d-x.

推荐答案

原帖是 http:/ /www.cocos2d-x.org/boards/6/topics/7006

我发现将sqlite包含在cocos2dx游戏中的最简单方法。

I found that a easiest way to include sqlite to cocos2dx game.

也就是说,从sqlite3 c ++ api下载源代码,并将sqlite3.c添加到Android.mk。

That is, download the source code from sqlite3 c++ api, and add sqlite3.c to Android.mk.

然后将这些代码编译为cocos2dx代码。

Then compile these code as your cocos2dx code.

并在需要使用时在代码中包含sqlite.h。

and include the sqlite.h in yourcode when you need to use it.

对数据库的操作以下是我的示例代码:

For operation on database following is my sample code:

sqlite3 *pDB = NULL;
char* errMsg = NULL;
string sqlstr;
int result;
string dbPath = CCFileUtils::getWriteablePath();
dbPath.append("Settings.db");
result = sqlite3_open(dbPath.c_str(),&pDB);
if (result != SQLITE_OK)
    CCLOG("OPENING WRONG, %d, MSG:%s",result,errMsg);

bool isExisted_;
sqlstr = "select count(type) from sqlite_master where type='table' and name='YourTableName'";
result = sqlite3_exec(pDB, sqlstr.c_str(), isExisted, &isExisted_, &errMsg);
if(result != SQLITE_OK)
    CCLOG("check exist fail %d Msg: %s", result, errMsg);
result = sqlite3_exec(pDB, "create table YourTableName(ID INTEGER primary key autoincrement, name varchar(32), type INT, posX INT, posY INT, isUnlock INT)",NULL,NULL,&errMsg);
if(result != SQLITE_OK)
    CCLOG("CREATE TABLE FAIL %d, Msg: %s",result,errMsg);
sqlite3_close(pDB);

这篇关于使用cocos2d-x的数据库(例如sqlite)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆