1. 用CCUserDefault存储简单数据
CCUserDefault *userDefault =CCUserDefault::sharedUserDefault();
if(!userDefault->getBoolForKey("isExisted")){
userDefault->setBoolForKey("isExisted",true);//不知道userDefault是啥?正文第五行,自己翻去
}
string user = userDefault->getStringForKey("user","");
if (user == "") {
userDefault->setStringForKey("user","allen");
userDefault->flush();// save data between sessions 就是说可以在关闭游戏后重新打开游戏时仍然可以读取数据
CCLog(CCString::createWithFormat("user not exsit, create player 'allen' .")->getCString());
pLabel->setString(CCString::createWithFormat("user not exsit, create player 'allen' .")->getCString());
}else {
string user = userDefault->getStringForKey("user","");
CCLog(CCString::createWithFormat("get user exsit!!")->getCString());
pLabel->setString(CCString::createWithFormat("get user exsited!!")->getCString());
}
2. 用sqlite3 存储复杂数据
见这个帖子:
http://article.ityran.com/archives/2793
但在用xcode 进行c++开发时需注意2点:
a.只需要copy sqlite3.h, sqlite3.c 和sqlite3ext.h,不要拷贝shell.h。否则编译会出错
b.在创建数据库的时候win32不需要指定路径,而xcode开发中需要指定路径:
// test SQLite3 database
sqlite3 *pDB = NULL;
char *errMsg = NULL;
string sqlStr;
int result;
// open db
string sqlPath = CCFileUtils::sharedFileUtils()->getWriteablePath() + "save.db";
CCLog(sqlPath.c_str());
result = sqlite3_open_v2(sqlPath.c_str(), &pDB, SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE, NULL);
if (result != SQLITE_OK) {
CCLog("open database failed, error code : %d, error message : %s\n", result, errMsg);
}
// create table
result = sqlite3_exec(pDB, "create table MyTable_1(ID integer primary key autoincrement, name nvarchar(32)) if not exsit", NULL, NULL, &errMsg);
if (result != SQLITE_OK) {
CCLog("create table failed, error code : %d, error message : %s\n", result, errMsg);
}
// insert data
result = sqlite3_exec(pDB, " insert into MyTable_1( name ) values ('allen')", NULL, NULL, &errMsg);
if (result != SQLITE_OK) {
CCLog("insert data failed, error code : %d, error message : %s\n", result, errMsg);
}