Android数据库SQLite使用详解三 : 数据库的升级


    上一篇博客:   Android数据库SQLite使用详解二 : 学生管理系统的简单实现


   我们知道,软件是有其生命周期的,我们不可能在一开始的时候就知道这个软件接下来所需要的所有数据库,而是在软件的更新迭代过程中,我们才根据不断变化的需求增加相应的数据库。

   当然,Android也给我们升级数据库提供了相应的方法 它就是 SQLiteOpenHelper 中的 public abstract void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion)
 方法


   我们来看看官方的注解: 


    (官方介绍)Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade 

to the new schema version.

   当数据库版本需要更新的时候会调用此方法,实现此方法来删除表,添加表或者做其他任何事情来使旧版本数据库更新成最新版本

    The SQLite ALTER TABLE documentation can be found here  (SQLite官方教程).If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table.

    如果你想在表中添加一个新的列可以使用  ALTER TABLE 直接将其插入到表中,如果你想重命名或者移除列,那么你可以使用  ALTER TABLE  来重命名旧表,然后创建一个新表在将旧表中的数据迁移到新表中

This method executes within a transaction. If an exception is thrown, all changes will automatically be rolled back.

这个方法在一个事务中执行(?,不知道什么意思,求大神告知),如果抛出了异常,那么所有的改变将会自动的还原


啰嗦了这么多,Android提供给我们的方法就是在更新表的时候,将旧表重命名为一个缓冲表,然后在创建一个符合此版本的新表(创建符合此版本的新表那么其实就是调用onCreate方法,即需要保证通过版本升级的数据库与第一次安装的数据库是相同结构的),将缓冲表中的数据迁移到新表中,然后在删掉缓冲表.

好了,上代码,此代码是更新了之前的学生管理系统的表结构,增加了一个性别列

/**
 * <p/>
 * SQLiteOpenHelper用于创建和管理数据库以及版本,需要创建一个子类继承,该类封装了对数据库操作的基本方法,使用方便。
 * <p/>
 * 此类即是对特定数据库进行封装的类,
 */
public class SqLiteHelper extends SQLiteOpenHelper {

    Context context;


    public SqLiteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);

        this.context = context;

        Log.i(StuDataBaseController.DBName,"执行了SqLiteHelper的初始化方法");

    }

    //调用此类的getReadableDatabase或getWritableDatabase时,
    //会判断是否有指定数据库存在,如果没有,则调用此onCreate方法
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {

        Log.i(StuDataBaseController.DBName,"执行了SqLiteHelper的onCreate方法");

        //创建学生表
        sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS " +
                StuModel.StuDataBaseTableName +
                " ( " + StuModel.StuTableId +
                " integer primary key," +
                StuModel.StuTableName +
                " VarChar(50)," +
                StuModel.StuTableAge +
                " integer,"+StuModel.StuTableSex+" integer" + " );");

    }


    //更新表的时候会调用,一般用于不同版本的软件更新数据库的时候使用

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {


       if (newVersion==2){

           //先重命名
           sqLiteDatabase.execSQL("alter table "+StuModel.StuDataBaseTableName+" rename to "+StuModel.StuDataBaseTableCacheName);

           //在调用自己的创建数据库表的方法
           onCreate(sqLiteDatabase);

           //在将数据移植过去
           sqLiteDatabase.execSQL("insert into "+StuModel.StuDataBaseTableName +" select *,'' from "+StuModel.StuDataBaseTableCacheName);

           //最后将缓冲表删除
           sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + StuModel.StuDataBaseTableCacheName);


       }

//      sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + StuModel.StuDataBaseTableName);
//
//      onCreate(sqLiteDatabase);


    }
}

之前的调用方法是这样 : 

helper = new SqLiteHelper(context,DBName,null,1);

现在因为需要这样调用

helper = new SqLiteHelper(context,DBName,null,2);

这样才能让 SqLiteHelper 知道他对应的版本号是2,如果手机里面的数据库版本是1的话,就会调用  onUpgrade  方法来更新数据库

 

只有这样,才能保证,每次得到的数据库,都是指定版本号的数据库

如果需要源程序,请留言,我会给你发过去的

 

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值