【问题标题】:Use database (such as sqlite) with cocos2d-x用 cocos2d-x 使用数据库(如 sqlite)
【发布时间】:2012-07-03 08:45:31
【问题描述】:

我开始在 iPhone 上构建游戏应用程序。我正在使用 cocos2d-x 游戏引擎,因为它很容易从那里移植到 android。编码也是用我非常熟悉的 C++ 编写的。我想知道是否有办法在 cocos2d-x 中使用任何数据库。虽然 sqlite 是首选但不是强制性的。我将在数据库中有大约 1/2 mb 的数据。所以,是的,我也考虑过保留/使用内存数据库,但我希望我的读/写查询能够节省时间。

我查看了一些博客,这表明我可能需要为 sqlite 使用 C++ 包装器。问题是,对于独立的 C++ 代码,我可以设置环境,但是如何将其集成到 xcode(在 mac os 中)以使用 sqlite 和 cocos2d-x。

【问题讨论】:

    标签: ios sqlite xcode4 cocos2d-x


    【解决方案1】:

    原帖在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);
    

    【讨论】:

    • 您说“Sqlite c++ api”,但我确实在 Sqlite 网站上找到了任何反映这一点的内容,并且我从它们那里获得的下载文件是 C(因此是 .c)而不是 C++。我错过了什么还是它们是同一个
    • @owengerig 这是我的问题。它实际上是c,你可以在c++代码中毫无问题地使用它。
    【解决方案2】:

    我认为最好的方法是:

    整个topis都在这里:http://www.cocos2d-x.org/boards/6/topics/7006

    示例代码:)

    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】:

      我写了一篇关于在xcode中逐步将sqlite集成到cocos2d-x游戏中的博客。你可以看到这个 http://sqlite-integration-in-cocos2d-x.blogspot.in/

      【讨论】:

      • SQLite 已经集成在 Android 和 IOS 中,您只需将其引用到您的项目中即可。
      • @ChaleCS cocos2d-x 兄弟的情况有点不同。如果你想只使用c++和cocos2d-x框架实现sqlite集成,而不需要进入原生ios(obj c)和android(java)代码,那么这篇博客会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-08
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      相关资源
      最近更新 更多