正在恢复 sqlite 数据库 android
Restoring sqlite database android
我正在尝试在我的应用程序中添加备份功能,但我遇到了一些问题。
这是我的代码:
public void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/" + "com.example" + "/databases/" + "clinic_database";
String backupDBPath = "clinic_database";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (true) {
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getActivity(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/" + "com.example" + "/databases/" + "clinic_database";
String backupDBPath = "clinic_database";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
123);
} else {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getActivity(), "DB saved! sdcard/" + backupDBPath, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "nope", Toast.LENGTH_SHORT).show();
}
}
我的数据库中有 3 个默认患者。
首先,我将我的数据库文件导出到 sdcard。
在下一步中,我从我的数据库中删除患者。
然后我点击导入按钮,一切看起来都很好(应用程序数据文件夹中的数据库文件已更新),但我的数据库中仍然没有任何患者。
可能是因为在我的应用程序中我有 3 个数据库文件:clinic_database、clinic_database-shm 和 clinic_database-wal,但我认为 shm 和 wal 文件是临时的。
那么,我是不是做错了什么?我应该 "reload" 我的数据库吗?
提前致谢!
Maybe it's because in my app I have 3 database files: clinic_database, clinic_database-shm and clinic_database-wal, but I think that shm and wal file are temporary.
虽然它们本身是临时的,但如果 -wal 文件和 -shm 文件包含任何事务,它们可能会持续存在,因此如果 -wal 文件保留,这些事务将被应用。
如果使用 WAL,备份数据库的正确方法是
备份 -wal 和 -shm 文件以及数据库并恢复所有 3 个文件。
或者确保数据库完全检查点,然后只备份数据库文件,恢复时确保删除 -wal 和 -shm 文件(如果存在)。
- 关闭数据库将完全检查数据库。
- 或者你使用 wal_checkpoint pragma to checkpoint。
我正在尝试在我的应用程序中添加备份功能,但我遇到了一些问题。
这是我的代码:
public void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/" + "com.example" + "/databases/" + "clinic_database";
String backupDBPath = "clinic_database";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (true) {
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getActivity(), "Database Restored successfully", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG)
.show();
}
}
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/" + "com.example" + "/databases/" + "clinic_database";
String backupDBPath = "clinic_database";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
int permissionCheck = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
123);
} else {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getActivity(), "DB saved! sdcard/" + backupDBPath, Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getActivity(), "nope", Toast.LENGTH_SHORT).show();
}
}
我的数据库中有 3 个默认患者。 首先,我将我的数据库文件导出到 sdcard。 在下一步中,我从我的数据库中删除患者。 然后我点击导入按钮,一切看起来都很好(应用程序数据文件夹中的数据库文件已更新),但我的数据库中仍然没有任何患者。
可能是因为在我的应用程序中我有 3 个数据库文件:clinic_database、clinic_database-shm 和 clinic_database-wal,但我认为 shm 和 wal 文件是临时的。
那么,我是不是做错了什么?我应该 "reload" 我的数据库吗? 提前致谢!
Maybe it's because in my app I have 3 database files: clinic_database, clinic_database-shm and clinic_database-wal, but I think that shm and wal file are temporary.
虽然它们本身是临时的,但如果 -wal 文件和 -shm 文件包含任何事务,它们可能会持续存在,因此如果 -wal 文件保留,这些事务将被应用。
如果使用 WAL,备份数据库的正确方法是
备份 -wal 和 -shm 文件以及数据库并恢复所有 3 个文件。
或者确保数据库完全检查点,然后只备份数据库文件,恢复时确保删除 -wal 和 -shm 文件(如果存在)。
- 关闭数据库将完全检查数据库。
- 或者你使用 wal_checkpoint pragma to checkpoint。