Hello everyone,
I was looking to be able to add tables and columns in a sqlite database, but on already downloaded applications.
I didn’t want users to lose their data, so I had to figure out how to do it.
So I’m sharing a few modest lines of code with you.
This analyzes database 1, and if any tables or columns are missing from table 2, they are created.
I’ve done this in particular for iOS applications where the base must be copied. This way, each time the application is updated, when the user downloads the new version, the database is updated without losing any data.
If ever these few lines of code can be useful to someone.
Have a nice day.
Thank you for sharing the code.
I’ve had the same situation and it took some time to figure out.
I like your approach of comparing two databases. Much nicer than what I did because I hard coded the tables and columns to add.
I will certainly use your code in a future update of my apps.
A few issues with the code you shared:
It would be better to have all database code in a Class that could just be copied over to a new project.
Currently your code is in the App class so each method needs to be copied one by one.
The database file in the copy build step is missing
In App.Opening event you are going to get some serious Exceptions not handling the failure of this line of code:
dbFile.CopyTo(dest)
You can’t imagine the amount of iPhone users that have no more space available on their device. Copying a file can fail at any time.
Calling messagebox from the App.Opening event might fail to display a message.
The database exception should come before the RuntimeException in this code:
Catch error As RuntimeException
MessageBox("Runtime Error: " + error.Message)
catch dberror as DatabaseException
MessageBox ("Database Error : " + dberror.Message)
You should do a check to see if SpecialFolder.Documents exists before using it.
if it doesn’t exist, create the folder then use it.
Hi Jeremie,
thank you for your valuable advice as always.
I’ve made a few changes accordingly.
Some exception handling added.
I’m sure there are lots of things to improve, but this approach meant I didn’t have to hard-code database updates.