Android--Interview--Database upgrade strategy

Source: Internet
Author: User

Upgrade: Overriding the Onupgrade method

    1. Determine the difference between adjacent versions, starting from version 1 to iterate over the update, first execute V1 to v2, then v2 to v3 ...
    2. For each version determine the difference from the current database, write a special upgrade code for each case.

Downgrade

Ondowngrade () database demotion: For example, downgrading from database 4 to database 3 must override this method.

@Override      Public void int int newversion) {        super. Ondowngrade (db, Oldversion, newversion);    }

Migrating data:

    1. Rename an existing table to a temporary table;
    2. Create a new table;
    3. Import data from the staging table into a new table (Note that the modified column is processed);
    4. Deletes a temporary table.
protected voidupgradetables (sqlitedatabase db, String tableName, string columns) {Try{db.begintransaction (); //1. Rename an existing table to a temporary tableString temptablename = tableName + "_temp"; String SQL= "ALTER TABLE" + tableName + "RENAME to" +Temptablename; EXECSQL (DB, SQL,NULL); //2, create a new table//oncreatetable (db);Createnewtablex (DB); //3. Import temporary table data into a new tablesql = "INSERT into" + TableName + "(" + Columns + ")" + "select" + Columns + "FR OM "+Temptablename; EXECSQL (DB, SQL,NULL); //4. Delete temporary tablesExecsql (db, "DROP TABLE IF EXISTS" + temptablename,NULL);      Db.settransactionsuccessful (); }      Catch(SQLException e) {e.printstacktrace (); }      Catch(Exception e) {e.printstacktrace (); }      finally{db.endtransaction (); }  }  

Android--Interview--Database upgrade strategy

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.