2012-12-13 135 views
-2

我想從我的應用程序中導出SQLite數據庫,以便我可以爲我的應用程序執行導出/導入功能。Android導出SQLite數據庫

我使用從here & here企圖出口SQLite數據庫(/data/data/com.example.worldcountriesbooks/databases/group.db)了Android設備的例子(三星Galaxy Note) ,安卓4.0.3但我不斷收到此錯誤

12-14 00:56:33.722: I/Failed(14850): java.io.FileNotFoundException: /data/data/com.example.worldcountriesbooks/databases/group.db: open failed: ENOENT (No such file or directory) 

我也試圖給WRITE_EXTERNAL_STORAGE & WRITE_MEDIA_STORAGE添加到清單文件,但它不工作。我能夠瀏覽到我的Android設備上的實際文件,因爲它是根植的,但我無法用我的應用程序讀取它。任何想法爲什麼?

示例代碼:

try { 
     File sd = Environment.getExternalStorageDirectory(); 
     File data = Environment.getDataDirectory(); 

     if (sd.canWrite()) { 
      String currentDBPath = "//data//com.example.worldcountriesbooks/databases//group.db"; 
      String backupDBPath = "//mnt//sdcard//database.db"; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 

      FileChannel src = new FileInputStream(currentDB).getChannel(); 
      FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
      dst.transferFrom(src, 0, src.size()); 
      src.close(); 
      dst.close(); 
      Toast.makeText(getBaseContext(), backupDB.toString(), 
        Toast.LENGTH_LONG).show(); 

     } 
    } catch (Exception e) { 

     Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG) 
       .show(); 
    } 

回答

1

如果該文件是在內存中,你的應用程序可以從內部存儲器中一個特殊的文件夾只讀。該文件夾的路徑是通過返回:

getFilesDir().getAbsolutePath() 

在你的情況下,它會是這樣的:

String path= this.getFilesDir().getAbsolutePath() + File.separator + "databases" + File.separator + "group.db"; 

你可以用openFileInput()閱讀。

更多信息:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

+0

所以沒有辦法,我可以從我的應用程序複製了SQLite數據庫? – dythe

+0

寫在SD卡上 – thaussma

+0

是的,我已經試圖寫它到我的SD卡,但它甚至不能打開'/數據/數據'內部數據庫寫入SD卡 – dythe