原始帖子位于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);