Upgrade Android app and keep old SQLite database

2019-08-17 05:06发布

问题:

I'm writting a small app for gym exercises. I keep my records in SQLiteDatabase and I want this app to start running as fast as I finish main part of the app. But further I want to develop this app and add some more features. There will be some records in a database yet. How can I save this records and move it to new version of the app? I'm developing with Android Studio, and each run app is installed from the scratch(before previous version is unistalled and all data is deleted?) if there were any changes, right? So, how can I keep old records and add them to new version app? I have seen some answers with onUpgrade, but it wasn't that specific that i can understand them, beacause they don't tell anything about upgrading only app, not the databse. And sorry for my English :)

回答1:

Installing a new version of your app doesn't necessarily remove its data (unless you uninstall it manually before installation or just clear data manually).

Also, if you change your database structure in an update, you can override SQLiteOpenHelper's onUpgrade method in order to handle database updates properly.