Management of version upgrade for Android SQLite Database
Http://angrycode.cn/archives/428
The version upgrade is involved in the android project, and the database will also lead to the increase and decrease of tables and fields due to the change of requirements. This is inevitable.
This problem was rarely considered when we started the project. However, when the software needs to be upgraded, the table structure of the database may change. If you do not consider upgrading the database version, when the software is upgraded, the database cannot be found.
Some fields have errors. You cannot delete databases of the previous version because data of the previous version is lost. Therefore, you need to carefully manage the database version.
Android provides sqliteopenhelper for developers to access the database. Developers only need to inherit this interface. It provides two external Methods: oncreate (sqlitedatabase dB) and
Onupgrade (sqlitedatabase dB, int oldversion, int newversion ). Both methods are automatically called by the system.
When the oncreate () method is used for the first time, the system will check whether the database file is used. if not, create a database table. If yes, check the database version number. If the current version number is higher than the previous version number, call onupgrade () to update the database table structure.
In the constructor of the sqliteopenhelper subclass, you need to pass a database version number starting from 1. With the version update, the number is constantly changing.
For example, if the database version of the software is 1 at the beginning, you need to change it to 2 at the next upgrade (of course, you only need to change it to a larger version than the previous version ).
The following is a simple example of angrycode:
<PRE> public class databasehelper extends sqliteopenhelper {/*** log tag */Private Static final string tag = "databasehelper "; /*** database name */Private Static final string db_name = "user_manager.db";/*** database version number, which starts from 1 by default and is upgraded to 2 on February 31,. When the database needs to be updated, change this value. */Private Static final int db_version = 2; Private Static databasehelper instance = new databasehelper (myapplication. getcontext (); Private databasehelper (context) {super (context, db_name, null, db_version);} private databasehelper (context, int version) {super (context, db_name, null, version);} public static databasehelper getdatabasehelper () {return instance ;}@ overridepublic void oncreate (sqlitedatabase dB) {// create user table db.exe csql ("create table if not exists user_table (ID integer primary key autoincrement, name varchar (32) not null)") ;}@ overridepublic void onupgrade (sqlitedatabase dB, int oldversion, int newversion) {// modify the table here // string sql1 = "Drop table if exists user_table ;"; string sql7 = "alter table user_table add column phone_number varchar (15); db.exe csql (sql7); // oncreate (db) ;}</PRE>