Updating database with Sqlite.net Pcl for Xamarin android

I got a problem: when a new version of my application is released, if I add a new column to one of my db tables, the database is not updated. Does anyone know how to create a script to update the version in case new columns or new tables appear

thank

+3


source to share


3 answers


You have to remember that CreateTable is already doing the column updates for you because internally it calls the MigrateTable method, which you can see here for further clarification: https://github.com/praeclarum/sqlite-net/blob/master/src/ SQLite.cs # L562 .

However, you may need to handle more complex changes to your database, such as adding triggers or something similar. In this case, I suggest that you make the changes manually.

In Xamarin Forms I ended up with this: https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396



May not be the best strategy ever, but seems to work for me.

To summarize :
You must store the version of the database in an internal SQlite database flag called user_version, accessible with the PRAGMA keyword. Every time you get a database connection, you need to check and see if the current version of the database is the same as the latest version of the application database. If not, you need to update the database and install the new current version.

-2


source


This is not a script question, as it doesn't. You can release a "patch" version that will run once by fetching all your records into a temporary form โ†’ deleting the table โ†’ creating it again (assuring it to create new columns, etc.) โ†’ re-inserting the record again. After a while, when you know that all of your users (or when you set the limit) have moved to a newer version, you can simply remove the "patch" from your code.



Hope it helps.

+2


source


The automatic migration feature still doesn't work in sqlite.net-pcl , but it looks like it works in another sqlite package: sqlite-net-pcl , which is actually the sqlite package recommended by Xamarin.

+1


source







All Articles