Update app with preloaded SQLite

▼魔方 西西 提交于 2020-01-16 01:22:14

问题


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?


回答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.




回答2:


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.



来源:https://stackoverflow.com/questions/6954529/update-app-with-preloaded-sqlite

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!