我想備份一個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();
}
}
您是否請求了寫入外部存儲的權限?另外,打印'currentDB'和'backupDB'路徑並確保它們是正確的 –
嘗試https://stackoverflow.com/a/13504743/4146722 –
[Android備份/恢復:如何備份內部數據庫?](https://stackoverflow.com/questions/5282936/android-backup-restore-how-to-backup-an-internal-database) – araknoid