Issues with upgrading or downgrading the SQLite database when the app version is updated

Source: Internet
Author: User
Tags sqlite database

SQLite is a small relational database built into Android. Sqliteopenhelper is an abstract class that is used to assist in managing database creation and version escalation issues. We can inherit this abstract class and implement some of its methods to customize the operation of the database. The following two methods must be overridden:

    • public void OnCreate (Sqlitedatabase db)
    • public void Onupgrade (sqlitedatabase db, int oldversion, int newversion)

In addition, the Sqliteopenhelper subclass must specify the name of the current database (name), version number (versions) when constructing the instance. The name here determines the name of the file when the database is stored, and there is no absolute association between the version number and the app's Versioncode defined in Androidmainfest.xml. That is, if the data structure of the database does not change when the app is updated, then the version number of the database does not increase.

OnCreate: The call is initiated after the user installs the app for the first time, or when the app database file is cleared. You can then complete the initial creation of the data table in this function.

Onupgrade: The call is made when the user makes an app update, overwrites the installation, and is called if the database version number in the new version is higher than the database version number in the old version. This can be done in this function to complete the old version of the database version of compatibility issues, as well as data migration issues.

There is also a general case where rewriting is not required, but the method that must be overridden when the application has a reverse demotion (such as applying a package with version number 3 that is downgraded by version number 4) Ondowngrade, if the application demotion overrides the method when overridden by the installation, crashes.

We may encounter situations where the database version is upgraded:

    • Fields that need to extend a table
    • Remove a redundant field from the original table
    • Create a new table

The process of dealing with these problems should be done without compromising the historical data of the old database. Here we assume that the user on the phone before the installation of the database version 1 of the package, the upgrade installed is the database version number 2 of the package. At this point we are going to handle these upgrade logic in a package with a database version of 2.

The first is the implementation of the field in Onupgrade that extends a table as:

Mydbhelper.java
1234567 @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { ///The old database version is 1 to add a student_name field for table pedant if (Oldversion < 2) {db.execsql (" ALTER TABLE pedant ADD COLUMN student_name text ");}}

SQLite support for ALTER TABLE is limited, you can add a field to the end of an existing table, or change the name of the table. But if you want to do something more complicated, such as deleting a field from a table, re-create the table and complete the data migration, rather than using a convenient command like DROP COLUMN . See SQLite frequently Questions

For example, table Pedant originally has three fields A, B, C, and now want to delete the C field, then the onupgrade in the following wording:

Mydbhelper.java
123456789101112131415161718 @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { //The old database version is 1, delete the C field of table Pedantif (Oldversion < 2) { Db.begintransaction ();try {Db.execsql ("CREATE temporary TABLE pe_backup (A, b);");Db.execsql ("INSERT into Pe_backup SELECT A, b from pedant;");Db.execsql ("DROP TABLE pedant;");Db.execsql ("CREATE TABLE pedant (a text, b text);");Db.execsql ("INSERT into pedant SELECT A, b from pe_backup;");Db.execsql ("DROP TABLE pe_backup;");Db.settransactionsuccessful ();}finally {Db.endtransaction ();}}}

This completes both deleting the C field and preserving the data on the original table.

In the last case, the simplest straightforward execution of the CREATE statement will be possible.

Mydbhelper.java
1234567 @Override public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { ///Old database version is 1, create new table NEWTBif (Oldversion < 2) {db.execsql ("CREATE Table NEWTB (a Text, b text); ");}}

When the database is being upgraded, we can clearly know what we are going to do with the old tables to be compatible with the new version. But if the database is degraded, the situation is not the same, for us to develop a new version 2 o'clock, we can not clearly know the future of the new version such as version 3, 4 of the database structure of how the trend. For example, later users fall back from version 3 to the version 2 we are developing, because we developed the table structure of version 3 is not foreseen at that time, I do not know whether version 3 of the data table can be compatible to version 2 (if a version 3 is removed when a version 2 has been used in the table field, the fallback data structure may be incompatible), Then we are in the development of version 2 o'clock the most secure way is to rewrite Ondowngrade when all the current version will be used to rebuild all the tables, that is, the demotion of all the previous data discarded .

Mydbhelper.java
12345678910 //Because we cannot predict future versions of the table structure, the surest way to backwards-compatibility is to refactor the table that the version needs for itself once @Override public void Ondowngrade (sqlitedatabase db, int oldversion, int newversion) { db.execsql ("DROP TABLE IF EXISTS t1;"); Db.execsql ("DROP TABLE IF EXISTS T2;"); Db.execsql ("DROP TABLE IF EXISTS T3;"); Db.execsql ("DROP TABLE IF EXISTS T4;"); ..... onCreate (DB); //Build Table }

Welcome reprint, please indicate the source link!!!

Issues with upgrading or downgrading the SQLite database when the app version is updated

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.