2014-11-08 100 views

回答

4

直接資產文件夾內,創建一個文件夾名稱爲「StreamingAssets」,並把您的SQLite數據庫文件在此文件夾中,然後使用下面的代碼來提取和複製的SQLite數據庫persistentDataPath:

void InitSqliteFile (string dbName) 
{ 
    // dbName = "example.sqlite"; 
    pathDB = System.IO.Path.Combine (Application.persistentDataPath, dbName); 
    //original path 
    string sourcePath = System.IO.Path.Combine (Application.streamingAssetsPath, dbName); 

    //if DB does not exist in persistent data folder (folder "Documents" on iOS) or source DB is newer then copy it 
    if (!System.IO.File.Exists (pathDB) || (System.IO.File.GetLastWriteTimeUtc(sourcePath) > System.IO.File.GetLastWriteTimeUtc(pathDB))) { 

     if (sourcePath.Contains ("://")) { 
      // Android 
      WWW www = new WWW (sourcePath); 
      // Wait for download to complete - not pretty at all but easy hack for now 
      // and it would not take long since the data is on the local device. 
      while (!www.isDone) {;} 

      if (String.IsNullOrEmpty(www.error)) {     
       System.IO.File.WriteAllBytes(pathDB, www.bytes); 
      } else { 
       CanExQuery = false;          
      } 

     } else { 
      // Mac, Windows, Iphone 

      //validate the existens of the DB in the original folder (folder "streamingAssets") 
      if (System.IO.File.Exists (sourcePath)) { 

       //copy file - alle systems except Android 
       System.IO.File.Copy (sourcePath, pathDB, true); 

      } else { 
       CanExQuery = false; 
       Debug.Log ("ERROR: the file DB named " + dbName + " doesn't exist in the StreamingAssets Folder, please copy it there."); 
      } 

     }   

    } 
} 
+0

應標作爲正確的答案。我一直在努力,但我猜StreamingAssets文件夾是關鍵。這麼多問題,很少。 – gdbj 2015-03-03 01:08:57

相關問題