Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sqlite

ios

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 content 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?

like image 578
Ascendant Avatar asked Jun 15 '11 05:06

Ascendant


1 Answers

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.

like image 165
gerry.liang Avatar answered Oct 25 '22 23:10

gerry.liang