开发者

Update app with preloaded SQLite

开发者 https://www.devze.com 2023-03-26 07:06 出处:网络
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 D

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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消