Updating new version to app store with different sqlite db structure Updating new version to app store with different sqlite db structure sqlite sqlite

Updating new version to app store with different sqlite db structure


sqlite supports something called as user_version property, you can execute PRAGMA user_version to query for current schema version of your app db. This query can happen write at the beginning when your app starts.

to update this user_version execute following update query PRAGMA user_version = version_num;

Whenever you create sqlite db, it is best practice to put this property user_version, so that when you are upgrading in future you can query current value. Check what it's needs to be and execute remaining alter or create tables to upgrade your schema version.

For example:

In first release I create table1 with col1, col2

I execute sql to create table1 and once it is successfully done, i execute pragma user_version = 1. so this will indicate my current schema version is 1

In future release i add new col3, i need to change my schema version to 2

I first query user_version, check it's value and if it is 1, then you need to run alter script to add new column and set user version to 2.

In your case, since you haven't set the user_version before, it would be difficult to differentiate new install vs an upgrade scenario. So for now may be you assume if db is present it is upgrade scenario and execute alter scripts and if not present assume it is a newinstall scenario and run create scripts. But see if you can use above pragma to solve your problem in future atleast.


You could check on launch if it's the old db, and if so have a routine to create a new db with the new structure (with a temporary name), copy all the data from the old to the new, close the old db & delete it, close the new db, rename it, then finally open it again for your updated app to use. Easy, fast, & you don't need an in-depth knowledge of SQLite to do it.