我有一个具有备份和恢复功能的应用程序。单击备份按钮会将 db 文件复制到外部存储。单击恢复按钮将从外部存储(如果存在)中获取文件并将其复制到应用程序中,用备份数据库文件覆盖现有数据库。
我在对表执行更新时遇到了问题。表更新数据后,更新数据的数据库不会复制到外部存储,或者如果我在更新后尝试恢复,则不会复制文件。
保存项目时,打开数据库,然后调用以下函数执行更新,然后关闭数据库。
public void saveItem(int ItemID, int ItemNumber, String itemnote)
{
ContentValues args = new ContentValues();
args.put("ItemNote", itemnote);
mDb.update("Items", args, "ItemID =" + Item+" and ItemNumber ="+ItemNumber, null);
发生更新后,是什么阻止复制 db 文件?
我正在使用以下代码将数据库备份导入应用程序。同样,这一切都在执行更新语句之前起作用。提前致谢。
importdb.setOnClickListener(new View.OnClickListener(){ public void onClick(查看 arg0) {
final File DATA_DIRECTORY_DATABASE = getDatabasePath("MyDB");
final File DATABASE_DIRECTORY = new File(Environment.getExternalStorageDirectory(),"/MyApp");
final File IMPORT_FILE = new File(DATABASE_DIRECTORY,"MyDB");
File exportFile = DATA_DIRECTORY_DATABASE;
File importFile = IMPORT_FILE;
try {
exportFile.createNewFile();
copyFile(importFile, exportFile);
Log.i("Import", "Succesfull");
} catch (IOException e) {
e.printStackTrace();
}
}
});
private static void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
最佳答案
我认为数据库版本没有改变,这就是代码无法正常工作的原因。
关于Android:当 wal 存在时执行数据更新后复制 db 文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514540/