I am using SQLite (SQLite without Core Data) in an app for the first time and I am ready to submit the app for approval but I have a question about handling database upgrades:
Question:
1. I am going to make the assumption that I will have to either add another column or another table to the SQLite database at some point in the future. Is there a way to properly manage future upgrades to the db by creating some sort of db versioning in my app from the very beginning?
For example if I currently have a table with 3 columns (id, FirstName, LastName) and I find out I need to add a column called JobTitle after the app is Live, how can I ensure that adding this new column will not crash the app when users upgrade?
I am trying to avoid any problems that may 'break' the database in a future release because I didn't properly prepare for handling upgrades from the beginning.
I am going to make the assumption that I will have to either add another column or another table to the SQLite database at some point in the future. Is there a way to properly manage future upgrades to the db by creating some sort of db versioning in my app from the very beginning?
Sure, I'm going to assume you have a method to check if the DB exists in your applications documents directory (so you can write to it) and if not creates it.
So what I did was add a table to this database, call it whatever you like. Add a column that represents the version of the database (an int - 0,1,2,3). Add a row that represents the db version for the application.
Each time you change the db in a release of your application increment the expected number.
If the existing number does not match the expected number, perform SQL to bring the DB up to spec. Remember the user may skip releases, so if 0 was the first version you might have someone that does not upgrade until version 3 - so version 3 needs to know how to go from 0, 1, or 2 to 3.
Sure, I'm going to assume you have a method to check if the DB exists in your applications documents directory (so you can write to it) and if not creates it.
So what I did was add a table to this database, call it whatever you like. Add a column that represents the version of the database (an int - 0,1,2,3). Add a row that represents the db version for the application.
Each time you change the db in a release of your application increment the expected number.
If the existing number does not match the expected number, perform SQL to bring the DB up to spec. Remember the user may skip releases, so if 0 was the first version you might have someone that does not upgrade until version 3 - so version 3 needs to know how to go from 0, 1, or 2 to 3.
Hi,
Thanks, it makes sense. Yes, I check if the DB exists, else I create it.
So when you have to, down the road, add a column to table 'x' (or create a new table) are you simply modifying your database.sqlite file and including that modified file in the app bundle OR are you doing something like this:
// current db version should be 3, the users app is still on version 2 so let's perform upgrade:
CREATE TABLE newtable.....
UPDATE TABLE oldtable1....
So when you have to, down the road, add a column to table 'x' (or create a new table) are you simply modifying your database.sqlite file and including that modified file in the app bundle OR are you doing something like this:
// current db version should be 3, the users app is still on version 2 so let's perform upgrade:
CREATE TABLE newtable.....
UPDATE TABLE oldtable1....
Regards,
Howcr
Well that depends if you want to preserve the data created by the user then SQL statements to modify the tables are in order. If you don't need to preserve the data then shipping a new database would be the way to go.
Well that depends if you want to preserve the data created by the user then SQL statements to modify the tables are in order. If you don't need to preserve the data then shipping a new database would be the way to go.
I added 8 columns to my database and no one had crashes. All I dud was check if the columns already exist. If they do not then I do an ALTER statement to add my new columns.
__________________
Haters gonna Hate
Likers gonna Like