简体   繁体   中英

How to update SQLite database when I didn't specify a version in my previous APK

I created my original app using Kivy for Python. Since then, I decided to port it over to Java and have re-built it in Android Studio. The app has an SQLite database that needs to be updated (new table, more entries, etc).

I saw the the onUpgrade() function can be used to do this, but it looks like the way it works is that I have to specify the old database version and then specify the new one I am updating with.

However when creating the original version, I don't specify a database version.

How do I then update the SQLite database within the app? For this version, I'm more than happy to delete the old database and add in the new one as it's currently not using any data specific to the user.

You only need to clone the table

CREATE TABLE copied AS SELECT * FROM mytable WHERE 0

Where mytable = 'Your table'

Also you can delete the table and recreate the scheme, but first you should do a backup

You have two possible solutions:

  1. find the current version of the database, reading a table in the database where you will store the database version). If the table does not exist (as in your actual app version), you can assume to be in the version 0 and perform any required upgrade process.
  2. delete the current database, as you are suggesting, and recreate it from scratch, adding the database version somewhere in the database.

To keep track of the database version, I suggest a Version table where you have just a few fields: VersionNumber , UpgradeDate .
Anytime you upgrade the database schema, you need to update the Version table with the version you have upgraded to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM