Android Database Upgrade

Source: Internet
Author: User

Database Upgrade: v1.0-v2.1
Upgrade from v1.0 to v2.01, do not go oncreate, go onupdategrade
Install v2.0 directly, go oncreate;

v1.0-v3.0: Three kinds of cases
1.v1.0 upgrade to v3.0: Don't go oncreate, go onupgrade
2.v2.0 upgrade to v3.0: Don't go oncreate, go onupgrade
3. Direct installation v3.0: Walk OnCreate, not go onupgrade
Public classDBHelperextendsSqliteopenhelper {//private static final int database_version = +; Private Static Final intDatabase_version = 1001; //private static final int database_version = 1002; Private Static FinalString database_name = "Mall.db"; Private StaticDBHelper instance =NULL; /*The Operational database has two very important classes: Sqlitedatabase and Sqliteopenhelper * 1.SQLiteDatabase examples represent the SQLite database, through some common methods of sqlitedatabase, Can execute SQL statements, * Add, delete, update, find and modify database operations
* 2.SQLiteOpenHelper is an auxiliary class that is primarily responsible for creating and opening databases and managing the version of the database. * Sqliteopenhelper is an abstract class that needs to inherit it and implement several of its callback functions **/   /*1. Write internal class inheritance Sqliteopenhelper 2. Build Implementation Method (1) Create public void OnCreate (Sqlitedatabase db) {(2) upgrade public voi D Onupgrade (sqlitedatabase db, int oldversion, int newversion) {3. Builder calls the constructor method of the parent class public DBHelper (String name, int v Ersion) {4. Click event Call construction method DBHelper Create connection Database*/    //internal class inheritance requires you to generate the constructor yourself     PublicDBHelper (Context context) {Super(Context, database_name,NULL, database_version); }    //Create an object (singleton mode)     Public synchronizedDBHelper getinstance (context context) {if(Instance = =NULL) {instance=NewDBHelper (context); }        returninstance; }    //Build Table@Override Public voidonCreate (sqlitedatabase sqlitedatabase) {sqlitedatabase.execsql (SQL.        Create_table_favorate); //perform a database upgrade directly if the first version is not installed//do not modify the value of First_database_version, which is the first database version size        Final intfirst_database_version = 1000;    Onupgrade (Sqlitedatabase, first_database_version, database_version); } @Override Public voidOnupgrade (Sqlitedatabase sqlitedatabase,intOldversion,intnewversion) {        //upgrading databases across versions using for         for(inti = oldversion; i < newversion; i++) {            Switch(i) { Case1000:                    //upgrade from v1.0 to v1.1upgradeToVersion1001 (sqlitedatabase);  Break;  Case1001:                    //upgrade from v1.1 to v1.2upgradeToVersion1002 (sqlitedatabase);  Break; default:                     Break; }        }    }    Private voidupgradeToVersion1001 (Sqlitedatabase db) {//Favorite Table 1 new fieldsString SQL1 = "ALTER TABLE" + SQL. T_favorite + "ADD COLUMN deleted VARCHAR";    Db.execsql (SQL1); }    Private voidupgradeToVersion1002 (Sqlitedatabase db) {//Favorite Table added 2 fields, add new field can only one field one field Plus, SQLite has a limit not allowed a statement plus multiple fieldsString SQL1 = "ALTER TABLE" + SQL. T_favorite + "ADD COLUMN message VARCHAR"; String SQL2= "ALTER TABLE" + SQL. T_favorite + "ADD COLUMN type VARCHAR";        Db.execsql (SQL1);    Db.execsql (SQL2); }}

Create a database (build a table)
 Public class SQL {    publicstaticfinal String t_favorite = "favorite";      Public Static Final String create_table_favorate= "CREATE TABLE t_favorite if not exists" +            "(_id Integer primary key autoincrement," +
    "title Varchar,url varchar," +            "creatadate varchar)";}

Android Database 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.