Android Database Version upgrade

Source: Internet
Author: User

After the Android app is released, the version iterations are made. Because of changes in requirements, the expansion of functionality, may involve the upgrade of the database. If you just add a table, it's good to handle it, and it's OK to create a new table directly in public void Onupgrade (sqlitedatabase db, int oldversion, int newversion), but it involves changes to the structure of the existing table. It is more troublesome, especially the structure of the table has been upgraded, when the upgrade needs to consider the user's current version, that is, in the code to the different versions of the database, upgrade to the latest version for processing. In fact, this is similar to making an incremental upgrade package, you need to make an incremental package for each historical version.

Although Csdnblog does not yet involve database upgrades, it is necessary to be prepared in advance. The next two days to learn the SQLite Database Upgrade Summary:

When the database does not exist, the Sqliteopenhelper's OnCreate () method is called, and then the application is started and the method is not called again if the database already exists. The corresponding method is called when the database version number is changed.

<span style= "White-space:pre" ></span>//construction Method Private DB (context context) {//CREATE database Super (context, db_name, NULL, database_version); LOG.I (TAG, "CREATE Database");}

The code above is an instance of creating Sqliteopenhelper (the DB class Inherits Sqliteopenhelper), where database_version is the custom database version number.

if Database_version is found to be less than the current database version number when the app starts , or the need to demote the database, the following method is called:

@Overridepublic void Ondowngrade (sqlitedatabase db, int oldversion, int newversion) {super.ondowngrade (db, Oldversion, newversion);}

Of course, I basically do not use the downgrade, I in the experiment, forget the current database version number, also did not overwrite the Ondowngrade method, give a version number, the run will appear unresponsive, or some other strange phenomenon.

When the database is upgraded, the Onupgrade method is called, in which we not only update the table structure, but also consider the data retention problem in the table. Of course, it would be nice to simply delete the old table and rebuild the table, but the data accumulated by the user is gone. In other words, we need to save the old table's data to the new table.

The idea is to change the table name of the old table first (if the table name changes after the upgrade), create a new table, and then reinsert the data into the new table based on the structure of the old table.

When version update is called @overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//field required to transfer data string columns = " "; LOG.I (TAG, "oldversion=" + oldversion);//backup of data based on different database versions switch (oldversion) {case 1:columns = Bloginfo.title + "," + Blo Ginfo.link + "," + Bloginfo.description + "," + Bloginfo.msg + "," + Bloginfo.datestamp + "," + Bloginfo.timestamp_read + ", "+ Bloginfo.content +", "+ Bloginfo.type_blog +", "+ bloginfo.type_article;break;case 2:break;default:db.execsql (" DROP Table if EXISTS "+ tb_blog);d b.execsql (" DROP table if EXISTS "+ Tb_bloger); onCreate (db);//create new table return;} Start preparing data try {db.begintransaction (); LOG.I (TAG, "Rename table."); String temptablename = tb_blog + "_temp"; String sql = "ALTER TABLE" + Tb_blog + "RENAME to" + temptablename;db.execsql (SQL); LOG.I (TAG, "Create table."); O  Ncreate (db);//Create New Table LOG.I (TAG, "Load data"); sql = "INSERT into" + Tb_blog + "(" + Columns + ")" + "select" + Columns + "From" + temptablename;db.execsql (SQL); LOG.I (TAG, "Drop the TEMPorary table. "); Db.execsql ("DROP TABLE IF EXISTS" + temptablename);d b.settransactionsuccessful ();d b.endtransaction ();} catch (Exception e) {}}

If the database has more than one historical version, it needs to be dealt with in switch one by one, of course, if it is not taken into account, it can only be rebuilt with ghastly removal.

One of them is to call the callback function directly OnCreate (db)

OnCreate (db);//Create a new table
Although it feels strange, it does work.


Because there is no time to use the database upgrade, so now just simple to implement the function, after the need to perfect.

If you have the guidance of the great God, thank you!

Android Database Version upgrade

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.