gpt4 book ai didi

android - 如何为非root设备恢复android中sqlite的数据库备份

转载 作者:行者123 更新时间:2023-11-30 00:46:39 24 4
gpt4 key购买 nike

我正在尝试将 bike_info.db 恢复到我的应用程序db 文件有一些记录,我想在应用程序中恢复它们

我尝试了下面的代码,显示 toast successful 但数据库没有恢复,我不知道为什么。谁能帮忙?

rest = (Button) findViewById(R.id.cview_restore);

rest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {

String currentDBPath = "//data//" + "com.infyco.kp.new_tab"
+ "//databases//" + "bike_info.db";
String backupDBPath = "//data//bike_info.db"; // From SD directory.
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);

FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Import Successful!",
Toast.LENGTH_SHORT).show();

}
} catch (Exception e) {

Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
.show();

}
}
});

点击 rest 按钮后,这是 android monitor 的输出:

01-17 14:00:40.922 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: ViewPostImeInputStage processPointer 0
01-17 14:00:40.962 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: ViewPostImeInputStage processPointer 1

01-17 14:00:41.012 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: #1 mView = android.widget.LinearLayout{9ba3ae9 V.E...... ......I. 0,0-0,0 #102039d android:id/toast_layout_root}

01-17 14:00:41.072 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1

01-17 14:00:43.022 8909-8909/com.infyco.kp.new_tab D/ViewRootImpl: #3 mView = null

最佳答案

这是工作恢复的核心恢复代码。一些区别:我在关门前冲水。

我重命名了原始数据库(制作副本并删除原始数据库)因此它不存在(如果恢复失败很容易恢复)。

我也在它自己的线程中执行此操作(此处未显示)。

我从系统获取数据库的路径/文件名。

                dbfile = new File(currentdbfilename);    
.......
try {
// Stage 1 Create a copy of the database
Log.i(logtag, "Stage 1 (make Copy of current DB)Starting");
FileInputStream fis = new FileInputStream(dbfile);
OutputStream backup = new FileOutputStream(copydbfilename);
while ((copylength = fis.read(buffer)) > 0) {
backup.write(buffer, 0, copylength);
}
backup.flush();
backup.close();
fis.close();
Log.i(logtag, "Stage 1 - Complete. Copy made of current DB.");
copytaken = true;

// Stage 2 - Delete the database file
if (dbfile.delete()) {
Log.i(logtag, "Stage 2 - Completed. Original DB deleted.");
origdeleted = true;
}

// Stage 3 copy from the backup to the deleted database file i.e. create it
Log.i(logtag, "Stage 3 - (Create new DB from backup) Starting.");
FileInputStream bkp = new FileInputStream(backupfilename);
OutputStream restore = new FileOutputStream(currentdbfilename);
copylength = 0;
while ((copylength = bkp.read(buffer)) > 0) {
restore.write(buffer, 0, copylength);
}
Log.i(logtag, "Stage 3 - Data Written");
restore.flush();
restore.close();
Log.i(logtag, "Stage 3 - New DB file flushed and closed");
restoredone = true;
bkp.close();
Log.i(logtag, "Stage 3 - Complete.");
} catch (IOException e) {
e.printStackTrace();
if(!copytaken) {
errlist.add("Restore failed copying current database. Error was " + e.getMessage());
} else {
if(!origdeleted) {
errlist.add("Restore failed to delete current database. Error was " + e.getMessage());
}
else {
if(!restoredone) {
errlist.add("Restore failed to recreate the database from the backup. Error was "+ e.getMessage());
errlist.add("Restore will attempt to revert to the original database.");
}
}
}
}

currentdbfilename 设置使用:-

            currentdbfilename = this.getDatabasePath(
DBConstants.DATABASE_NAME)
.getPath();

copydbfilename 是通过微调器从可用备份列表中选择的(略有不同的是备份位于公共(public)外部存储中)。

关于android - 如何为非root设备恢复android中sqlite的数据库备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41692316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com