2017-09-13 99 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中使用它,截至目前我在加载程序中捕获此路径活动