Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update SQLite DB on iPhone app update?

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.

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.

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?

Thanks!

like image 840
Billy Avatar asked May 04 '10 18:05

Billy


People also ask

How do you access SQLite using iOS mobile application?

Step 1 − Open Xcode -→ Single View Application -→ Let's name is DBSqlite. Step 2 − Let's develop our UI, Open Main. storyboard and add one text field and two buttons as shown below. Step 3 − Create @IBAction for both the buttons and @IBOutlet for text field and name them btnInsert, btnShowData and name respectively.

How SQLite manage data in iOS apps?

Step 1 − Create a simple View based application. Step 2 − Select your project file, then select targets and then add libsqlite3. dylib library in choose frameworks. Step 3 − Create a new file by selecting File→ New → File...

How can I see SQLite database in iOS?

Open DBBrowser and open database and in finder press CMD+SHIFT+G and paste your link there and press ENTER. You will be redirected to that location. 4. Select your SQLite file and it will be opened in DBBrowser.

Does SQLite update?

Introduction to SQLite UPDATE statement First, specify the table where you want to update after the UPDATE clause. Second, set new value for each column of the table in the SET clause. Third, specify rows to update using a condition in the WHERE clause. The WHERE clause is optional.


1 Answers

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.

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.

like image 115
Simon Woodside Avatar answered Nov 15 '22 18:11

Simon Woodside