使用cocos2d-x的数据库(例如sqlite)

Him*_*Him 5 sqlite ios xcode4 cocos2d-x

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

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

m.d*_*ing 2

原始帖子位于http://www.cocos2d-x.org/boards/6/topics/7006

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

即从sqlite3 c++ api下载源码,并将sqlite3.c添加到Android.mk中。

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

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

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

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);
Run Code Online (Sandbox Code Playgroud)