我想备份一个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