Search code examples
iosswiftsqlitefmdb

Comparing multiple SQLite databases with FMDB


I am writing a quiz-style app which is populated from a SQLite database through FMDB. I have copied the database from the app bundle to the app documents directory so that I have read/write privileges and can therefore save user data such as scores to the database as well.

However, I need a way to compare the database in the app documents with the database in the app bundle, so that updates to the bundle db (e.g. new questions) can be copied to the docs db. Simply copying the entire bundle db over to app docs isn't an option as this would overwrite previously saved user data. I had some sort of differences statement in mind such as

SELECT questionID FROM Bundle.Master EXCEPT SELECT questionID FROM AppDocs.Master

to look at what had changed and go from there, but I'm unsure how to use the executeQuery command with more than one db simultaneously.

Any thoughts/alternative approaches appreciated.


Solution

  • You could ship your bundled db with a "version", and copy this version along with other tables in the documents db. On app start up, compare the version of the bundled db with the version of the documents db, and perform a new import if necessary.

    You'd have all the opportunities to perform any db migration and (re)import exactly as it needs to be done.