Use database (such as sqlite) with cocos2d-x

只愿长相守 提交于 2019-12-05 00:10:12

问题


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.


回答1:


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



回答2:


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: http://www.cocos2d-x.org/boards/6/topics/7006

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

if(cos == NULL)
    {
    LOG("can't open");
            return;
    }
else
    LOG("I have something!");

string path = fileUtils->getWriteablePath();
path += "test_OUT";

char buffer[300];
sprintf(buffer,"PATH: %s\n",path.c_str());
LOG(buffer);
std::fstream outfile(path.c_str(),std::fstream::out);
outfile.write((const char*)smth,size-1);
outfile.close();

LOGN("Size:",size);
LOG((const char*)smth);



回答3:


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



来源:https://stackoverflow.com/questions/11307404/use-database-such-as-sqlite-with-cocos2d-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!