cocos2d-x 3.0rc2中读取sqlite文件

cocos2d-x 3.0rc2中读取sqlite文件的方式,在Android中直接读取软件内的会失败。须要复制到可写的路径下


sqlite3* dbFile = NULL;

std::string path;

    path = FileUtils::getInstance()->fullPathForFilename("db1.db");
    
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
    path  = FileUtils::getInstance()->getWritablePath();
    path  += "/db1.db";
    
    FILE* file = fopen(path.c_str(), "r");
    if (file == nullptr)
    {
        ssize_t size;
        const char* data = (char*) FileUtils::getInstance()->getFileData("db1.db", "rb", &size);
        file = fopen(path.c_str(), "wb");
        fwrite(data, size, 1, file);
        CC_SAFE_DELETE_ARRAY(data);
    }
    fclose(file);
    
#endif

CCLOG("数据库路径:%s", path.c_str());
int resultOK = sqlite3_open(path.c_str(), &dbFile);

if (resultOK != SQLITE_OK) {
    sqlite3_close(dbFile);
    CCLOG("数据库打开失败: %d", resultOK);
    return;
}


posted @ 2016-04-22 18:35  mfrbuaa  阅读(325)  评论(0编辑  收藏  举报