【发布时间】:2013-01-28 08:34:28
【问题描述】:
我有一个具有备份和恢复功能的应用程序。单击要备份的按钮会将 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();
}
}
【问题讨论】:
-
经过进一步审查和测试,这似乎是特定于设备的问题。我在其他设备上测试没有任何问题。出现此问题的设备是 HTC Droid Incredible。当对数据库进行更新时,我注意到数据库处于 wal 模式(预写日志记录)。我在 db 文件上进行了导出或恢复,我相信它会被 wal 模式覆盖,这就是为什么它的行为不像我想要的那样。关于如何处理 wal 模式的任何建议?