0

This is my first time working on a Xamarin App and I am new to the app development world so I need some help figuring out this process. Currently I run a php web service that generates some SQL files that I run in DB Browser and I get a database file which I then put into my Assets and Resources Folder. Using each platform's API I copy the database into a writable folder and use that to run my queries.

I followed this really helpful tutorial and it worked perfectly fine. https://medium.com/@hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 . After the "initial" setup I store a timestamp in a local table and and the next time the user opens the app I pass that timestamp and retrieve data that is older than that timestamp. The I update that timestamp and continue the process. That data is sent back in JSON format and make the updates to the tables.

My only concern is if a new version were to come out where I add a new table or a new column which is not present in the current version of my Database, how should I take care of those update Web Service calls? Is there a way of monitoring my DB version? I read somewhere where I could just ignore the new data that is not present already, like table or columns, but I'm not really sure how to do that. I also saw that if I call CreateTable on my current tables I could potentially update them?

Also for future reference each time I develop a new app would I need to regenerate a new database file to store in the assets/resources folder? Is there a more automated process for this? Along with monitoring the version of my database?

Any Help/Tutorials/Suggestions would be greatly appreciated.

jay2020
  • 13
  • 4

1 Answers1

0

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

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.

Reference here.

Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • Thank you for answering. In other words Lets say version 2 comes out at I have generated a new db file with a newer version than the one I have in my app. I can update my local table in my app. Hmm I think that's doable on my end if I can place the new db file in the server, and have my app check the version . However since I am storing my db locally in the folders. how could I bring down that new db file? Or are you suggesting returning like a JSON string with the data in it? – jay2020 Nov 04 '20 at 17:11