2017-09-13 91 views
-1

我想備份一個android sqlite數據庫。如果我運行代碼創建一個文件夾,我在program.But中給出的文件夾是空的,請幫我備份我的數據庫,我如何查看它。我正在使用Firefox的sqlite manager.Help我備份我的數據庫時,我點擊一個按鈕備份數據庫。如何備份SQLite數據庫?

我的代碼:

public void exportDatabase(String databaseName) { 

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

     if (sd.canWrite()) { 
      String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+""; 
      String backupDBPath = "PRIM.sqlite"; 
      File currentDB = new File(data, currentDBPath); 
      File backupDB = new File(sd, backupDBPath); 
      System.out.println("@@ Enter 12222222222222"); 
      if (currentDB.exists()) { 
       FileChannel src = new FileInputStream(currentDB).getChannel(); 
       FileChannel dst = new FileOutputStream(backupDB).getChannel(); 
       dst.transferFrom(src, 0, src.size()); 
       src.close(); 
       dst.close(); 
       System.out.println("@@ Enter 2"); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 
+0

您是否請求了寫入外部存儲的權限?另外,打印'currentDB'和'backupDB'路徑並確保它們是正確的 –

+0

嘗試https://stackoverflow.com/a/13504743/4146722 –

+0

[Android備份/恢復:如何備份內部數據庫?](https://stackoverflow.com/questions/5282936/android-backup-restore-how-to-backup-an-internal-database) – araknoid

回答

0

這是我拍攝的路徑,在平板電腦上它應該是有一個SD卡

 File removable = ContextCompat.getExternalFilesDirs(this, null)[1]; 
     THE_PATH = String.valueOf(removable) 

THE_PATH所有設備正常工作的外置SD卡是一種公共靜態字符串,所以我可以在我的DBHelper中使用它,截至目前我在加載程序中捕獲此路徑活動