android - 使用动态文件名获取 SQLite 数据库备份
问题描述
如果有多个.db
文件,如何获取最新的 SQLite 数据库(在下面的屏幕截图中,我要获取 11408102018000005)已备份到 SD 卡上?
笔记:
文件类型是动态的。所以图像上的最后一个数字是递增的。
这是我在恢复 SQLite 数据库时的代码。
private void restoreDB() {
try {
File[] sd = getContext().getExternalFilesDirs(null);
if (sd[1].canWrite()) {
File backupDB = new File(sd[1] + "/" + curControlNo); //<--- what to put here. This is just a test path and name
File currentDB = new File(getContext().getDatabasePath(DBHelper.DB_NAME).getPath());
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getContext(), "Import Successful!", Toast.LENGTH_SHORT).show();
openHome();
}
} catch (Throwable e) {
Toast.makeText(getContext(), "Import Failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
解决方案
您需要创建文件并传递要保存的数据库名称,如下所示:
File backupDB = new File(sd[1], DBHelper.DB_NAME);
希望它会奏效。
推荐阅读
- batch-file - 将多个文本文档附加到一个文档的批处理文件
- flutter - Updating state in flutter
- javascript - How to render the landing page in server and the rest in client side?
- c - 为什么 %c 之前需要空格才能使代码正常工作?
- gerrit - How to hide some compiled files from showing up in gerrit review
- lua - 如何将字符串转换为变量名?
- html - 元素不遵守流量,并在浏览器中的意外位置呈现
- javascript - How to place text inside each bar of the chart using d3
- angular - Angular Input Date display from datatable
- flutter - Connection refused error when creating Http server using dart