public static final String TEMP_SQL_CREATE_TABLE_SUBSCRIBE = "alter table "
+ A + " rename to temp_A";
原来的表结构是:
private static final String SQL_CREATE_TABLE_SUBSCRIBE = "create table if not exists "
+ A + "(id integer primary key autoincrement,code text not null,name text,username text)";
2.然后把备份表temp_A中的数据copy到新创建的数据库表A中,这个表A没发生结构上的变化
public static final String INSERT_SUBSCRIBE = "select 'insert into A (code,name,username,tablename)
values ('''||code||''','''||name||''',''cnki'','''||tablename||'''')' as insertSQL from temp_A";
public static final String[] arrWhereAct = {
"where code ='K174' and tablename = 'phonepaper'",
"where code ='GMRB' and tablename = 'newspaper'",
"where code ='XJSJ' and tablename = 'magazine'",
"where code ='JTKL' and tablename = 'magazine'" };
4.删除备份表
public static final String DELETE_TEMP_SUBSCRIBE = "delete from temp_A ";
public static final String DROP_TEMP_SUBSCRIBE = "drop table if exists temp_A";
5.然后把数据库版本号改为比之前高的版本号,在OnUpgrade方法中执行上述语句就行,具体如下:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
for (int j = oldVersion; j <= newVersion; j++) {
switch (j) {
case 2:
//创建临时表
db.execSQL(TEMP_SQL_CREATE_TABLE_SUBSCRIBE);
//执行OnCreate方法,这个方法中放的是表的初始化操作工作,比如创建新表之类的
onCreate(db);
//删除之前的表里面的那4条默认的数据
for (int i = 0; i < arrWhereAct.length; i++) {
db.execSQL(DELETE_TEMP_SUBSCRIBE + arrWhereAct);
}
//将临时表中的数据放入表A
Cursor cursor = db.rawQuery(INSERT_SUBSCRIBE, null);
if (cursor.moveToFirst()) {
do {
db.execSQL(cursor.getString(cursor
.getColumnIndex("insertSQL")));
} while (cursor.moveToNext());
}
cursor.close();
//将临时表删除掉
db.execSQL(DROP_TEMP_SUBSCRIBE);
break;
default:
break;
}
}
}
为什么要在方法里写for循环,主要是考虑到夸版本升级,比如有的用户一直不升级版本,数据库版本号一直是1,而客户端最新版本其实对应的数据库版本已经是4了,那么我中途可能对数据库做了很多修改,通过这个for循环,可以迭代升级,不会发生错误。
public class MyDbHelper1 extends SQLiteOpenHelper{
public final static int DB_VERSION = 1;
public final static String DB_NAME = "mydb.db";
public final String TABLE_NAME = "tbl_data";
public final String COL_UID = "uid";
public final String COL_ITSVALUE = "itsvalue";
public final String COL_CREATEDDATE = "createddate";
public MyDbHelper1(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table ["+TABLE_NAME+"] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')) )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}}
我们看到,在这里,我们的onCreate方法里,写个建表的语句,这个表有3个字段。
我们注意看下数据库的版本
于是到了五月份,由于业务需要,我们想添加新的字段到这个表里。我们这样写代码:
public class MyDbHelper2 extends SQLiteOpenHelper{
public final static int DB_VERSION = 2;
public final static String DB_NAME = "mydb.db";
public final String TABLE_NAME = "tbl_data";
public final String COL_UID = "uid";
public final String COL_ITSVALUE = "itsvalue";
public final String COL_CREATEDDATE = "createddate";
public final String COL_DESC = "desc";
public MyDbHelper2(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建新的数据库时。已经 有新列desc了。
String sql = "create table ["+TABLE_NAME+"] ( [uid] int identity primary key,[itsvalue] nvarchar(200),createddate TIMESTAMP default (datetime('now', 'localtime')),[desc] nvarchar(300) )";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(oldVersion == 1 && newVersion == 2){
//从版本1到版本2时,增加了一个字段 desc
String sql = "alter table ["+TABLE_NAME+"] add [desc] nvarchar(300)";
db.execSQL(sql);
}
}
}
这个时候,onCreate方法里,是新的建立表的方式。而不同的是 onUpgrade方法里我们检测了这样的变化,如果 是从版本1到版本2,我们执行了一段”添加列的sql语句“。这段代码,仅仅在符合这样的场景里执行。
通过上面的方式,我们就完成了一次的数据库升级的操作。android会判断 数据库的版本号,并自动的调用onUpgrade方法。