Android备份/恢复:如何备份内部数据库? [英] Android backup/restore: how to backup an internal database?

查看:268
本文介绍了Android备份/恢复:如何备份内部数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用提供的 FileBackupHelper 实现了 BackupAgentHelper 来备份和还原本机数据库。这是通常与 ContentProviders 一起使用的数据库,位于 / data / data / yourpackage / databases / 中。

I have implemented a BackupAgentHelper using the provided FileBackupHelper to backup and restore the native database I have. This is the database you typically use along with ContentProviders and which resides in /data/data/yourpackage/databases/.

我们会认为这是一种常见的情况。但是文档不清楚该怎么做: http://developer.android .com / guide / topics / data / backup.html 没有 BackupHelper 专门用于这些典型的数据库。因此,我使用 FileBackupHelper ,指向我的.db文件在 / databases / 对我的 ContentProviders 中的任何数据库操作(例如 db.insert )引入锁,甚至尝试创建 code> / databases / 之前的目录onRestore(),因为它在安装后不存在。

One would think this is a common case. However the docs aren't clear on what to do: http://developer.android.com/guide/topics/data/backup.html. There is no BackupHelper specifically for these typical databases. Hence I used the FileBackupHelper, pointed it to my .db file in "/databases/", introduced locks around any db operation (such as db.insert) in my ContentProviders, and even tried creating the "/databases/" directory before onRestore() because it does not exist after install.

我已经在过去的不同应用程序中成功地为 SharedPreferences 实现了类似的解决方案。然而,当我在模拟器2.2测试我的新实现,我看到一个备份执行到 LocalTransport 从日志,以及正在执行的还原(和 onRestore()调用)。

I have implemented a similar solution for the SharedPreferences successfully in a different app in the past. However when I test my new implementation in the emulator-2.2, I see a backup being performed to LocalTransport from the logs, as well as a restore being performed (and onRestore() called). Yet, the db file itself is never created.

请注意,这一切都是在安装之后,并且在第一次启动应用程序之前,已执行恢复。除此之外,我的测试策略是基于 http://developer.android。 com / guide / topics / data / backup.html#Testing

Note that this is all after an install, and before first launch of the app, after the restore has been performed. Apart from that my test strategy was based on http://developer.android.com/guide/topics/data/backup.html#Testing.

请注意,我不是在谈论一些我管理的sqlite数据库,关于备份到SDcard,自己的服务器或其他地方。

Please also note I'm not talking about some sqlite database I manage myself, nor about backing up to SDcard, own server or elsewhere.

我在文档中提到了关于使用自定义 BackupAgent 的数据库的提示,

I did see a mention in the docs about databases advising to use a custom BackupAgent but it does not seem related:


但是,如果需要,您可能需要直接扩展
BackupAgent:
*一个数据库。如果你有一个SQLite数据库,
想要恢复,当用户
重新安装你的应用程序,你需要
来构建一个自定义的BackupAgent,
读取相应的数据
备份操作,然后创建
表,并在
恢复操作期间插入数据。

However, you might want to extend BackupAgent directly if you need to: * Back up data in a database. If you have an SQLite database that you want to restore when the user re-installs your application, you need to build a custom BackupAgent that reads the appropriate data during a backup operation, then create your table and insert the data during a restore operation.

有些明确。

如果我真的需要自己做到SQL级别,那么我担心以下主题:

If I really need to do it myself up to the SQL level, then I'm worried about the following topics:


  • 打开数据库和事务。我不知道如何在应用程序工作流程之外通过这种单例类关闭它们。

  • Open databases and transactions. I have no idea how to close them from such a singleton class outside of my app's workflow.

如何通知用户备份正在进行,数据库被锁定。可能需要很长时间,因此我可能需要显示进度栏。

How to notify the user that a backup is in progress and the database is locked. It might take a long time, so I might need to show a progress bar.

如何在恢复时执行相同操作。据我所知,恢复可能发生在用户已经开始使用应用程序(并将数据输入数据库)。因此,您不能假定只是恢复备份的数据(删除空或旧数据)。

How to do the same on restore. As I understand, the restore might happen just when the user has already started using the app (and inputting data into the database). So you can't presume to just restore the backupped data in place (deleting the empty or old data). You'll have to somehow join it in, which for any non-trivial database is impossible due to the id's.

如何在还原后刷新应用程式

How to refresh the app after the restore is done without getting the user stuck at some - now - unreachable point.

我可以确定数据库已经在备份或还原时升级了吗?

Can I be sure the database has already been upgraded on backup or restore? Otherwise the expected schema might not match.

推荐答案

创建自定义 BackupHelper

public class DbBackupHelper extends FileBackupHelper {

    public DbBackupHelper(Context ctx, String dbName) {
        super(ctx, ctx.getDatabasePath(dbName).getAbsolutePath());
    }
}

,然后将其添加到 BackupAgentHelper

public void onCreate() {
    addHelper(DATABASE, new DbBackupHelper(this, DB.FILE));
}

这篇关于Android备份/恢复:如何备份内部数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆