@Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
  if (oldVersion != newVersion) {
    if (newVersion == 2 && oldVersion == 1) {
      WXLogUtils.d(TAG_STORAGE,"storage is updating from version " + oldVersion + " to version "+ newVersion);
      boolean updateResult=true;
      try {
        long start=System.currentTimeMillis();
        db.beginTransaction();
        String SQL_ADD_COLUMN_TIMESTAMP="ALTER TABLE " + TABLE_STORAGE + " ADD COLUMN "+ COLUMN_TIMESTAMP+ " TEXT;";
        WXLogUtils.d(TAG_STORAGE,"exec sql : " + SQL_ADD_COLUMN_TIMESTAMP);
        db.execSQL(SQL_ADD_COLUMN_TIMESTAMP);
        String SQL_ADD_COLUMN_PERSISTENT="ALTER TABLE " + TABLE_STORAGE + " ADD COLUMN "+ COLUMN_PERSISTENT+ " INTEGER;";
        WXLogUtils.d(TAG_STORAGE,"exec sql : " + SQL_ADD_COLUMN_PERSISTENT);
        db.execSQL(SQL_ADD_COLUMN_PERSISTENT);
        String SQL_UPDATE_TABLE="UPDATE " + TABLE_STORAGE + " SET "+ COLUMN_TIMESTAMP+ " = '"+ sDateFormatter.format(new Date())+ "' , "+ COLUMN_PERSISTENT+ " = 0";
        WXLogUtils.d(TAG_STORAGE,"exec sql : " + SQL_UPDATE_TABLE);
        db.execSQL(SQL_UPDATE_TABLE);
        db.setTransactionSuccessful();
        long time=System.currentTimeMillis() - start;
        WXLogUtils.d(TAG_STORAGE,"storage updated success (" + time + "ms)");
      }
 catch (      Exception e) {
        WXLogUtils.d(TAG_STORAGE,"storage updated failed from version " + oldVersion + " to version "+ newVersion+ ","+ e.getMessage());
        updateResult=false;
      }
 finally {
        db.endTransaction();
      }
      if (!updateResult) {
        WXLogUtils.d(TAG_STORAGE,"storage is rollback,all data will be removed");
        deleteDB();
        onCreate(db);
      }
    }
 else {
      deleteDB();
      onCreate(db);
    }
  }
}

@ Override public void onUpgrade ( SQLiteDatabase db , int oldVersion , int newVersion ) { if ( oldVersion != newVersion ) { if ( newVersion == 2 && oldVersion == 1 ) { WXLogUtils . d ( TAG_STORAGE , "storage is updating from version " + oldVersion + " to version " + newVersion ) ; boolean updateResult = true ; try { long start = System . currentTimeMillis ( ) ; db . beginTransaction ( ) ; String SQL_ADD_COLUMN_TIMESTAMP = "ALTER TABLE " + TABLE_STORAGE + " ADD COLUMN " + COLUMN_TIMESTAMP + " TEXT;" ; WXLogUtils . d ( TAG_STORAGE , "exec sql : " + SQL_ADD_COLUMN_TIMESTAMP ) ; db . execSQL ( SQL_ADD_COLUMN_TIMESTAMP ) ; String SQL_ADD_COLUMN_PERSISTENT = "ALTER TABLE " + TABLE_STORAGE + " ADD COLUMN " + COLUMN_PERSISTENT + " INTEGER;" ; WXLogUtils . d ( TAG_STORAGE , "exec sql : " + SQL_ADD_COLUMN_PERSISTENT ) ; db . execSQL ( SQL_ADD_COLUMN_PERSISTENT ) ; String SQL_UPDATE_TABLE = "UPDATE " + TABLE_STORAGE + " SET " + COLUMN_TIMESTAMP + " = '" + sDateFormatter . format ( new Date ( ) ) + "' , " + COLUMN_PERSISTENT + " = 0" ; WXLogUtils . d ( TAG_STORAGE , "exec sql : " + SQL_UPDATE_TABLE ) ; db . execSQL ( SQL_UPDATE_TABLE ) ; db . setTransactionSuccessful ( ) ; long time = System . currentTimeMillis ( ) - start ; WXLogUtils . d ( TAG_STORAGE , "storage updated success (" + time + "ms)" ) ; } catch ( Exception e ) { WXLogUtils . d ( TAG_STORAGE , "storage updated failed from version " + oldVersion + " to version " + newVersion + "," + e . getMessage ( ) ) ; updateResult = false ; } finally { db . endTransaction ( ) ; } if ( ! updateResult ) { WXLogUtils . d ( TAG_STORAGE , "storage is rollback,all data will be removed" ) ; deleteDB ( ) ; onCreate ( db ) ; } } else { deleteDB ( ) ; onCreate ( db ) ; } } }
version 1 : - - - - - - - - - - - - - - - - | key | value | - - - - - - - - - - - - - - - version 2 : - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | key | value | timestamp | persistent | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
