1

My app uses a preloaded and copied from /assets to data/data ( Ship an application with a database) db which is simply a single table of product data and is 'read only' as users do not save to the DB. If I add more products to the DB table I need to get this to existing users. If I update my app with the new DB will the update process delete the old DB that was copied from the assets dir to data/data thereby allowing the 'DBexists' check to fail on first running the updated version thus triggering copying of the new DB from /assets to data/data?

Community
  • 1
  • 1
Graham
  • 153
  • 5
  • 15

2 Answers2

1

Short answer, yes, if you put the following snippet it the onUpgrade() method:

try {
    copyDataBase("database.db");
} catch (IOException e) {
    Log.w(TAG, e);
}

It may be worth deleting the db file in copyDataBase() before writing over it, just to make it less likely to corrupt.

NB: this uses the implementation as used in the accepted answer of the question you linked.

Alex Curran
  • 8,818
  • 6
  • 49
  • 55
  • Having now re-read the upgrade part of the linked question there is every chance that I would mess that up. My DB is only ever likely to be a max of 200k so I will copy it every time the app is run. Will this do the delete in data/data? File dir = getFilesDir(); File file = new File(dir, "database.db"); boolean deleted = file.delete(); – Graham Aug 07 '11 at 08:52
  • Yup, I think so, but be careful to catch the error (`FileNotFoundException`) in the case the db doesn't exist. I've found with my db that it is really easy for it to get corrupted/not copy properly, so also be careful and check for that. – Alex Curran Aug 07 '11 at 14:33
0

Off the top of my head the only thing I can think of is to slightly abuse the onUpgrade() method in your SQLiteOpenHelper implementation, and change the database by making a call to this with a new version number and having it do whatever you need it to do in that method.

Saying that, I don't really know much about the Android app update process, so this could be way off.

epochengine
  • 2,072
  • 17
  • 21