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.
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.
original post is at http://www.cocos2d-x.org/boards/6/topics/7006
I found that a easiest way to include sqlite to cocos2dx game.
That is, download the source code from sqlite3 c++ api, and add sqlite3.c to Android.mk.
Then compile these code as your cocos2dx code.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With