Use database (such as sqlite) with cocos2d-x

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.

  • Problems adding Projects to Workspace in Xcode 4
  • “Data Model Version Compile”.. error after upgrading to Lion (Xcode4.1)
  • Permission Denied on Xcode 4
  • XCode: Could not launch “APP_X_Y” - 'A' packet returned an error: -1
  • Is there a way to jump to a specific method in Xcode?
  • How to change Xcode build setup without modifying project files?
  • 3 Solutions Collect From Internet About “Use database (such as sqlite) with cocos2d-x”

    original post is at

    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

    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();
    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);

    I think the best way is:

    • Open file with CCFileUtils::getFileData() you will get your file data.
      For example your sqlite file 🙂

    • Use CCFileUtils::getWriteablePath() to get directory where you can write.

    • Write your data to previous directory for example with fstream

    • Now you can access to your database with source code from sqlite3 c++ api.

    The whole topis is here:

    Example code 🙂

    CCFileUtils* fileUtils = CCFileUtils::sharedFileUtils();
    unsigned long size = 5;
    unsigned char* smth;
    smth = fileUtils->getFileData("koalalocker","r",&size);
    printf("Size: %lu\n\n",size);
    if(cos == NULL)
        LOG("can't open");
        LOG("I have something!");
    string path = fileUtils->getWriteablePath();
    path += "test_OUT";
    char buffer[300];
    sprintf(buffer,"PATH: %s\n",path.c_str());
    std::fstream outfile(path.c_str(),std::fstream::out);
    outfile.write((const char*)smth,size-1);
    LOG((const char*)smth);

    I have written a blog regarding integration of sqlite in a cocos2d-x game in xcode step by step. You can see this