Android的sqlite的备份/恢复,而不覆盖 [英] Android sqlite backup/restore without overwriting

查看:358
本文介绍了Android的sqlite的备份/恢复,而不覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在简短形式的问:这从我或许应该强调和简化我的问题的核心followups看起来。其核心是:似乎为Android DB的其他备份选项离开这个恢复可以在数据库目前覆盖数据的风险。这是这样了,有没有办法来备份,而这种风险/恢复?

在长期形成的问:已经经过很多的(而众多的)问题,看如何在Android备份了一个SQLite数据库我有一个问题,我无法找到答案<。 / p>

所有其它的备份/恢复处理的数据库文件保存到SD(或者,在<一个讨论href=\"http://stackoverflow.com/questions/26212460/how-to-backup-restore-sqlite-database-on-android-to-dropbox\">How Android上的备份/恢复SQLite数据库到Dropbox的,云),然后在需要时恢复它。我担心的是,那不是恢复覆盖当前DB?

我而言,当用户有新的安装程序,他们已经使用了很短的时间(产生新的数据)的应用程序,然后想从应用程序的previous备份导入数据有关。与其他所有的备份/恢复,好像恢复旧的数据库文件将在当前DB文件覆盖任何新数据的方法。我想反而是一个备份选项,在恢复,会从备份的数据添加到当前数据库,使其完全没有在它覆盖任何东西。

做其他的方法做到这一点?还是他们,因为我怀疑,在这种情况下覆盖?

如果他们这样做覆盖,然后我最好的备份选择可能是写出到CSV或XML文件或东西,我预计这些备份的讨论是关于简单的方法可以做到这一点。是否有任何进程共建,以加快这一进程,并可以很容易或者说我必须手动尽一切?如果是的话,就建议的格式写,为什么?

同样,没有人知道是否使用BackupAgentHelper将有同样的覆盖问题,内置在谷歌备份?

最后,如果我最终通过数据迁移会(类似于<一个href=\"http://stackoverflow.com/questions/12397877/how-to-restore-sqlite-database-from-backup-after-core-data-model-has-changed-li\">How之后,核心数据模型已经改变(轻量级迁移))在任何一点什么,我现在应该怎么办(我仍然在数据库设计阶段),从备份恢复SQLite数据库做出这样一个潜在的未来变化更容易VIS-à -vis这个备份过程?


解决方案

我想恢复旧数据不改变(或矛盾含)问题'新'的数据是不是很辛苦在你所描述的情况来解决。看来,基本上你只是想与旧数据有较新的数据没有逻辑冲突(即,它是语义确定以创建新的记录的假设添加旧的数据(记录)到新的数据库旧数据)。我相信,恢复过程中采取的主键冲突的照顾是要考虑的最重要的问题。

让我们分两种情况:

1:您正在使用自动生成数据库的主键值特征(例如,使用的SQLite表的自动增量列)

让我们假定'新'的数据库记录可能已经使用主键(ROWID)的1值10恢复处理开始之前。如果你有任何的主键值(1〜10)的旧的数据库记录,你有问题只有当你想preserve那些古老的主键值。为了避免这样的问题时,不要挽留老的主键an'old的价值 - 旧的纪录从读取记录后''数据库,只需保存的其他属性(列)的值老纪录并让数据库生成此恢复记录一个新的主键值。从本质上讲,旧的记录将保存一个新的主键值。如果你担心这个恢复过程后,保持的记录的时间顺序,我建议你也保持其价值不会在过程中得到改变timestamp列。

2:您正在使用生成主键值的替代机制(UUID,序列发生器等):

在这种情况下,读旧的记录,并保存在'新'数据库之前,用备用机制产生的主键值取代旧的主键值 - 那会,由设计,保证在将恢复记录的主键值的唯一性与对于pre-现有的新的记载。

要简化这一恢复过程的编程工作,特别是如果你正在处理多个表,你可以使用一个ORM喜欢的 JDXA 。一个随SDK中的示例应用程序的说明了在升级数据库到新版本传输旧的数据类似的技术。 JDXA还提供了一个方便的序列发生器的机制来轻松,高效地创建,你可以坚持之前分配给你的对象唯一的ID。

Question in short form: It seems from the followups that I should perhaps emphasize and simplify the core of my question. The core is this: other backup options for Android DBs seems to leave the risk that a restore could overwrite data currently in the database. Is this so, and is there a way to backup/restore without this risk?

.

Question in long form: Having looked through many of the (rather numerous) questions about backing up up an SQLite db on Android I have one question that I couldn't find an answer to.

All the other backup/restore discussions dealt with saving the db file to the SD (or, in How to backup/restore SQLite database on Android to Dropbox, to the cloud), and then restoring it when required. My concern is, wouldn't that restore overwrite the current DB?

I'm concerned about when the a user has a new install of an app they've been using for a short time (generating new data) and then want to import data from a previous backup of the app. With all the other backup/restore approaches it seems like restoring the old DB file would overwrite any new data in the current DB file. What I want instead is a backup option that, on restore, would add the data from the backup into the current DB to make it complete without overwriting anything else in it.

Do the other approaches do this? Or do they, as I suspect, overwrite in such a case?

If they do overwrite then my best backup option is probably to write out to a csv or xml file or something and I expected these backup discussions to be about easy ways to do that. Are there any processes build to speed that process and make it easy or do I have to do all that manually? If so, recommendations on the format to write to and why?

Similarly, does anyone know if the built in Google backup using BackupAgentHelper would have this same overwrite issue?

And finally, if I end up going through a data migration (similar to How to Restore SQLite Database from Backup after Core Data model has changed (lightweight migration)) at any point what should I do now (I'm still in DB design stage) to make such a potential future change easier vis-à-vis this backup process?

解决方案

I think the problem of restoring old data without changing (or conflicting with) the ‘newer’ data is not very hard to solve in the scenario you describe. It seems that essentially you just want to ‘add’ the old data (records) to the new database with the assumption that the old data has no logical conflict with the newer data (that is, it is semantically OK to create new records for the old data). I believe that taking care of the primary key conflicts during restoration would be the most important issue to consider.

Let’s take two cases:

1: You are using the auto generation of primary key values feature of the database (e.g., using an AUTOINCREMENT column of a SQLite table).

Let’s assume that the ‘newer’ database records might have used primary key (ROWID) values of 1 to 10 before the restoration process is started. If you have ‘older’ database records with any of those primary key values (1 to 10), you have a problem only if you want to preserve those old primary key values. To avoid that problem, don’t retain the ‘old’ primary key value of an‘old’ record – after reading the record from the ‘old’ database, just save the values of other attributes (columns) of the ‘old’ record and let the database generate a new primary key value for this ‘restored’ record. Essentially, the ‘old’ record is saved with a new primary key value. If you are concerned about maintaining a chronological order of the records after this ‘restoration’ process, I suggest you also keep a timestamp column whose value does not get changed in the process.

2: You are using an alternate mechanism (UUID, Sequence Generators, etc.) for generating primary key values:

In this case, read the ‘old’ record and before saving it in the ‘newer’ database, replace the ‘old’ primary key value with a primary key value generated with the alternate mechanism – that would, by design, guarantee the uniqueness of the primary key value of the ‘restored’ record with respect to the pre-existing ‘newer’ records.

To simplify the programming effort for this ‘restoration’ process, especially if you are dealing with multiple tables, you may use an ORM like JDXA. One of the sample apps shipped with the SDK illustrates a similar technique for transferring ‘old’ data while upgrading a database to a newer version. JDXA also offers a convenient sequence generator mechanism to easily and efficiently create unique ids that you can assign to your objects before persisting them.

这篇关于Android的sqlite的备份/恢复,而不覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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