views:

152

answers:

2

Hi,

I have an App using sqlite. On first start, I copy the .db file into NSDocumentDirectory (so that I can make updates to it). In later versions, I plan to add new data to this database. How can I make sure, that with every application update (but not with every app start) the newest copy of this DB will be copied to NSDocumentDirectory?

Thanks

-Konstantin

+1  A: 

I have a constant that I increment with new builds, say kDatabaseVersion.

At startup, I check for the following:

  • Does the DB exist in the Documents directory? If not, copy it. This probably means a first launch has occurred.

  • If the DB does exist, check the version from the constant against a NSUserDefaults entry with the same key. If the constant is greater, copy the database over. If not, do not. Update NSUserDefaults accordingly.

Of course, if the database contains data from your users also, you have to work out how to migrate that to a new data store. If you are using Core Data, you might even consider multiple persistent stores to separate user and default data.

Hunter
A: 

Keep a database version number in your App. When the app starts, check if the database exists in document directory. If not, copy it to the doc. directory. If the database already exists, compare the database version number from your app with the number stored in a database table. if the numbers are equal everything is fine, otherwise you have to "upgrade" the existing database (modify database schema or whatever). So you can upgrade the database with every version of your app. Code a simple function "CheckForDatabaseUpdate" that contains all the neccessary logic. And make a "UpgradeToDatabaseVersion" function with a version number as parameter. This function will handle the upgrade of the database schema from one version to another.

Goeran