Search code examples
androidxamarinupgradeportable-class-librarysqlite.net

Upgrade database with Sqlite.net Pcl for Xamarin android


i have the problem that when i release new version of my application, if i add a new column to one of my db tables, the database doesn't update. Any one know how to create a script of upgrade versione in case there are new columns or new tables??

Thanks


Solution

  • You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable.

    However you could have to handle more advanced modification to your database, like adding triggers or something similar. In that case i suggest you to perform modifications manually.

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

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

    Summarizing :
    You have to save the database version in an internal flag of the SQlite database called user_version accessible with PRAGMA keyword. Every time you get the database connection, you have to perform a check and see if the current database version is the same as the app last database version. If not you need to perform a database update and set the new current version.