开发者

How to overwrite a SQLite DB in iOS when a new version of app is released

I have an iOS app that uses a SQLite DB to store its data model. The user cannot change the contents of this DB in any way. The only way contents of the DB will change is when I add more content in future app updates. (The app never writes con开发者_如何学运维tent to the DB, only reads from it)

So, when I perform such an update, I want it to use the "newer" version of the DB in the new version I am creating, and to just get rid of the old DB. What's the best way to go about this? Is there a simple way to just tell it to grab the new version of the DB when I update, or do I need to program in custom logic for that?


Let's discuss from the beginning of your app been installed for the first time. The sqlite db will be init in the flowing steps:

  1. Check if the [appname].db exist in the app install folder.

  2. Create an empty [appname].db file in the install folder if this file does not exist.

  3. Create a record to indicate the current app version and store it in the table named 'appversion'(this also can be stored in preference file).

  4. Load the old version number from the location where you store it in step 3, compare it to the current version number,and you need to execute serval sql files named like '[appname]_sql_v1.sql,[appname]_sql_v2.sql,[appname]_sql_v3.sql',Keep in mind that ,each edition will own a '[appname]_sql_v*.sql' file that contains all the table schema changes and records changes.if your app have been upgrade to version 7,you will find 7 sql files in you app package.

  5. Let's assume that some guy have installed a app versioned 2,but did not update until one day he/she find the current version is 7,and then he/she upgrade it to 7. Two code version number will be loaded when he/she started the app for the first time after the upgrade finished:2 and 7,so these sql files:[appname]_sql_v3.sql,[appname]_sql_v4.sql,[appname]_sql_v5.sql,[appname]_sql_v6.sql,[appname]_sql_v7.sql will be execute one by one.

Remember these things:

1.do not put sql init statement in code,put it in a sql file and read it to sqlite db as needed.

2.Each edition will owns a '[appname]_sql_v*.sql' file that contains the changes made to the pervious version.


If you use your DB from your bundle (I assume you do as your DB is read-only), then you automatically use a new version of DB for an app update. If you copy your DB from the bundle to the documentation folder, then just rewrite your old DB with new one.


Add the preloaded database to your project (to the Resources folder). This will be deployed with your application, and can be found at runtime in the main bundle path.

Since the user will never write content to it but only read from it, you should be able to open it straight from there. You can get the path for it using:

NSString *databasePath = [[NSBundle mainBundle] pathForResource:@"ReadOnlyDatabase" ofType:@"sqlite"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜