3.x的sqlite在Android读写失败

Apr 5, 2015


  • 问题描述如题,解决方法:直接在软件内读取会失败,需要复制到可写路径内

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