例如一个数据库表
19版本是2个字段
20版本是3个字段 21版本是4个字段
package com.yzc.atclient.service.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper;import com.allcam.atclient.app.ClientApplication;
public class DatabaseHelper extends SQLiteOpenHelper
{ private static final String DB_NAME = "allcam_db";private static final int VERSION = 1;
private static volatile DatabaseHelper instance;
public DatabaseHelper(Context context)
{ this(context, DB_NAME, null, VERSION); }public DatabaseHelper(Context context, String name, CursorFactory factory, int version)
{ super(context, name, factory, version); }public static DatabaseHelper getInstance()
{ if (null == instance) { synchronized (DatabaseHelper.class) { if (null == instance) { instance = new DatabaseHelper(ClientApplication.getAppContext()); } } } return instance; }@Override
public void onCreate(SQLiteDatabase db) { CardUserTable.createTable(db); TaskStatusTable.createTable(db); }@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //在这里组合case 判断 升级 } }
例如:
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[i]); } //将临时表中的数据放入表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; } }