我在资产中有sqlite文件,dbflow正确读取它,但在运行时我有时需要下载 来自互联网的新更新的sqlite文件,并将其放在SD卡的某个文件夹中 所以如何通过sdcard中的sqlite文件更新dbflow数据库 删除所有表的数据 并将sdcard文件中的新数据添加到它们?
答案 0 :(得分:0)
尝试BElow Code。这是我在卸载后从Application for Restoring Data导入的备份。试试代码。希望它有效
注意:在运行时提供阅读和写入许可并且在显示中显示
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = DOWNLOADEDDATAPATH; //SOURCE
String backupDBPath = "data/" + getContext().getPackageName() + "/databases/FILENAME"; //Destination
File backupDB = new File(sd, currentDBPath); //SOURCE
File currentDB = new File(data, backupDBPath); //Destination
FileChannel src = new FileInputStream(backupDB).getChannel(); //SOURCE
FileChannel dst = new FileOutputStream(currentDB).getChannel(); //Destination
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {
Log.d("test", e.toString());
}
}