I have an app I need to update with new data for the DB. There’s nothing else there to update, no schema changes, no code changes. All I want to do is add some rows to a DB that the user can’t modify.
Normally on first install, I copy the DB from assets/. After that, when the program runs, I check whether the DB file exists in /data/data/my.package/databases and if it does, I don’t copy a second time.
Now, the new version of the app will have the full DB intact, and I want to add the new rows for those who have the old version. I have a few options:
- Use the
onUpgrade()function ofSQLiteOpenHelperto insert the rows. I don’t want to do this because there are many rows, and I’d rather put them in theassets/folder to be imported. Is there a way to accessassets/from withinonUpgrade()? - Delete and recreate the DB on every launch. This is wasteful, but it ensures that the full DB is there no matter what previous version of the app was installed.
- Do a check to see the previous version of the app that was installed and selectively delete the DB at first launch to have the DB with new rows copied from
assets/only on the first launch. For this, I can’t quite figure out what I should check. There is no way to know the old app version when running. I thought of usingPackageInfo.firstInstallTimeorPackageInfo.lastUpdateTimeand checking for a time where, if the user installed/updated before that, then they obviously have the first version I’m looking for. I know that’s error-prone, but I’ve taken the app off the market right now, so I have some leeway here.
An alternative way of phrasing this questions is: If I’m upgrading an app and I have some files in /data/data/my.package/* that I either want to delete or update, based on the version change, how can I accomplish that since I can’t know what previous version was installed?
The
onUpgradecallback tells you both the old and new versions.