如何在iPhone应用更新上更新SQLite DB? [英] How to update SQLite DB on iPhone app update?

查看:113
本文介绍了如何在iPhone应用更新上更新SQLite DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在iTunes应用商店中有一个使用SQLite数据库作为数据存储区的iPhone应用。用户可以将应用程序与Web服务同步,然后将返回的数据存储在SQLite数据库中。用户还可以将新项目插入数据库并将其更改同步回Web服务。

I currently have an iPhone app in the iTunes app store that uses a SQLite database as a datastore. The user can sync the app against a web service and data that is returned is then stored in the SQLite database. The user can also insert new items into the database and sync their changes back up to the web service.

当连接打开到应用程序的本地数据库时,代码检查确认数据库文件是否存在于Documents文件夹中,如果不存在,则通过从Resources文件夹复制到Doc​​uments文件夹来创建数据库。

When a connection is opened to the app's local database, the code checks to confirm that the database file exists in the "Documents" folder, if it does not the database is created by being copied from the "Resources" folder to the "Documents" folder.

我必须发布应用程序的更新。此更新包含数据库架构更改(新表,列等)。根据我的阅读,Documents目录中的文件将保留在应用更新中,这意味着更新后现有数据库仍然完好无损。

I have to release an update to the app. The update contains database schema changes (new tables, columns, etc.). From what I have read, files in the "Documents" directory will persisted on an app update, meaning the existing database will still be intact after the update.

我的问题是,如何用我更新的数据库替换现有数据库,有没有办法在不丢失现有数据库中的数据的情况下这样做?是否存在某种更新后首次运行事件,我可以使用它来进行数据库更新,或者我是否必须对现有数据库进行某种检查(查找新列或表)以及检查是否失败手动更换/更新数据库?

My question is, how do I replace the existing database with my updated database and is there a way to do so without losing the data in the existing database? Is there some sort of "first run after update" event I can work with to do the database update, or do I have to do some sort of check on the existing database (look for a new column or table) and if my check fails manually replace/update the database?

谢谢!

推荐答案

你应该将应用程序的当前版本号存储在某个地方 - 在数据库中或者更好地存储在Defaults数据库中。启动时,您的应用会将其自己的版本号与持久版本号进行比较。如果它们不同,则这是更新后的第一次运行。如果不存在持久版本号,那么显然这也是更新后的第一次运行。

You should store the current version number of the app somewhere persistent -- in the database or better yet in the Defaults database. On startup, your app will compare it's own version number with the persisted version number. If they differ, then this is the first run after an update. If the persisted version number isn't there, then obviously also this is the first run after an update.

至于更新数据库,如果它已到位你会使用通常的SQL ALTER命令来更新架构,并同时进行任何数据库数据迁移。

As for updating your database, if it's in place you would use the usual SQL ALTER commands to update your schema, and do any database data migration at the same time.

这篇关于如何在iPhone应用更新上更新SQLite DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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