问题描述
我实现了一个 BackupAgentHelper
使用所提供的 FileBackupHelper
备份和恢复本地数据库中,我有。这是通常使用与 ContentProviders
键,它驻留在数据库 /数据/数据/ yourpackage /数据库/
。
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文件中的 /数据库/
,周围的任何DB操作引入锁(如 db.insert
)在我的 ContentProviders
,甚至试图打造 /数据库/
目录之前, 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.
我已经实现了共享preferences
成功在过去不同的应用程序类似的解决方案。然而,当我在模拟器中测试-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数据库我管理自己,也无法左右备份到SD卡,自己的服务器或其他地方。
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的 读取期间适当的数据 备份操作,然后创建您 表,并插入过程中的数据 还原操作。
有些清晰吧。
如果我真的需要做我自己到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.
如何做还原相同。据我了解,恢复可能会发生只是当用户已经开始使用该应用程序(和输入数据到数据库)。所以你不能presume刚刚恢复到位backupped数据(删除空的或旧的数据)。你必须以某种方式加入它,这对于任何不平凡的数据库是不可能的,因为该ID的。
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
:
A cleaner approach would be to create a custom 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的备份/恢复:如何备份内部数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!