Android database version upgrade, android database version

Source: Internet
Author: User

Android database version upgrade, android database version

After the android Application is released, it performs version iteration. The Database Upgrade may be involved due to the change of requirements and the expansion of functions. If you only want to add a table, it is better to deal with it. You can directly create a table in public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion, however, it is troublesome to change the structure of an existing table, especially the structure of the table that has been upgraded before. When upgrading the table again, you must consider the current version of the user, that is, you need to upgrade the database of different versions to the latest version in the code. In fact, this is similar to creating an incremental update package. You need to create an incremental package for each historical version.

Although CSDNBlog does not involve Database Upgrade, it is necessary to prepare it in advance. Next we will summarize the sqlite database upgrades learned over the past two days:

When the database does not exist, the onCreate () method of SQLiteOpenHelper will be called; then the application will be started. If the database already exists, this method will not be called again. When the database version number changes, the corresponding method is called.

<Span style = "white-space: pre"> </span> // constructor private DB (Context context) {// create a database super (context, DB_NAME, null, DATABASE_VERSION); Log. I (TAG, "create database ");}

The code above is to create an instance of SQLiteOpenHelper (SQLiteOpenHelper inherited by the DB class), where DATABASE_VERSION is the custom database version number.

If the application finds that DATABASE_VERSION is earlier than the current database version, the database must be downgraded and the following method is called:

@Overridepublic void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {super.onDowngrade(db, oldVersion, newVersion);}

Of course, the downgrade function is basically not used. During the test, I forgot the current database version number and did not overwrite the onDowngrade method. I just gave a version number and no response will appear after running, or some other strange phenomena.

During Database Upgrade, The onUpgrade method is called. In this method, we not only need to update the table structure, but also consider the data retention issue in the table. Of course, it would be nice to directly Delete the old table and recreate the table, but the data accumulated by the user is gone. That is to say, we need to save the data of the old table to the new table.

The idea is: first change the table name of the old table (if the table name changes after upgrade, this step is not required), create a new table, then, the data is inserted into the new table based on the structure of the old table.

// Called during version update @ Overridepublic void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// String columns = ""; Log. I (TAG, "oldVersion =" + oldVersion); // switch (oldVersion) {case 1: columns = BlogInfo. TITLE + "," + BlogInfo. LINK + "," + BlogInfo. DESCRIPTION + "," + BlogInfo. MSG + "," + BlogInfo. DATESTAMP + "," + BlogInfo. TIMESTAMP_READ + "," + BlogInfo. CONTENT + "," + BlogInfo. TYPE_BLOG + "," + BlogInfo. TYPE_ARTICLE; break; case 2: break; default: db.exe cSQL ("drop table if exists" + tb_blog1_mongodb.exe cSQL ("drop table if exists" + TB_BLOGER); onCreate (db ); // create a new table return;} // start to prepare the data try {db. beginTransaction (); Log. I (TAG, "Rename table. "); String tempTableName = TB_BLOG +" _ temp "; String SQL =" alter table "+ TB_BLOG +" rename to "+ temptablenamemongodb.exe cSQL (SQL); Log. I (TAG, "Create table. "); onCreate (db); // create a new table Log. I (TAG, "Load data"); SQL = "INSERT INTO" + TB_BLOG + "(" + columns + ") "+" SELECT "+ columns +" FROM "+ temptablenamemongodb.exe cSQL (SQL); Log. I (TAG, "Drop the temporary table. "drop table if exists" + tempTableName); db. setTransactionSuccessful (); db. endTransaction ();} catch (Exception e ){}}

If the database has multiple historical versions, it needs to be processed in the switch. Of course, if you do not consider the situation, you can only delete and recreate it.

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

OnCreate (db); // create a new table
Although it seems strange, it does.


Because database upgrades have not yet been used, the function is now simply implemented and will be improved as needed.

Thanks for your guidance!

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.