Android SQLite Database Upgrade

Source: Internet
Author: User

Scene:

Database Version V1 has a persion table

Database version v2 new table student

Database version v3 persion table A new field

1  PackageCom.example.natedb;2 3 ImportAndroid.content.Context;4 Importandroid.database.SQLException;5 Importandroid.database.sqlite.SQLiteDatabase;6 Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;7 ImportAndroid.database.sqlite.SQLiteOpenHelper;8 9  Public classSqlDbextendsSqliteopenhelper {Ten  One     Private Final Static intVersion = 3; A      PublicSqlDb (Context context) { -          This(Context, "lihao.db",NULL, version); -     } the      PublicSqlDb (Context context, String name, Cursorfactory factory, -             intversion) { -         Super(context, name, Factory, version); -          +     } -  + @Override A      Public voidonCreate (Sqlitedatabase db) { at         //TODO auto-generated Method Stub -     //db.execsql ("CREATE TABLE person (_id integer PRIMARY key autoincrement,name text,age text)"); -         //Latest -Db.execsql ("CREATE TABLE person (_id integer primary key autoincrement,name text,age Text,score txt)"); -Db.execsql ("CREATE TABLE student (_id integer PRIMARY key autoincrement,name text,age text)"); -     } in  - @Override to      Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { +         //TODO auto-generated Method Stub -         intCurrentVersion =oldversion; the db.begintransaction (); *         Try { $             if(CurrentVersion = = 1) {Panax NotoginsengDb.execsql ("CREATE TABLE student (_id integer PRIMARY key autoincrement,name text,age text)"); -CurrentVersion = 2; the             } +  A             if(CurrentVersion = = 2) { theString temptable = "Person_temp"; +                 //Modify the original table as a temporary table -Db.execsql ("ALTER TABLE person rename to" +temptable); $                  $                 //Create a new table -Db.execsql ("CREATE TABLE person (_id integer primary key autoincrement,name text,age Text,score txt)"); -                  the                 //Copying Data -Db.execsql ("INSERT into person (name,age) Select Name,age from" +temptable);Wuyi                  the                 //Delete temporary table -Db.execsql ("drop table" +temptable); Wu             } - db.settransactionsuccessful (); About}Catch(SQLException e) { $             //TODO auto-generated Catch block - e.printstacktrace (); -}finally{ - db.endtransaction (); A         } +          the          -     } $  the      the}

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