Room数据库升级

  • 首先在继承RoomBase的类当中,定义Migration
    •   
          static Migration migration = new Migration(1,2) {
              @Override
              public void migrate(@NonNull SupportSQLiteDatabase database) {
                  database.execSQL("alter table student_table ADD  COLUMN begin_time INTEGER NOT NULL DEFAULT 0");
              }
          };
  • RooDataBase().addMigrations(migration).build
    •   
      Room.databaseBuilder(mContext.getApplicationContext(),WorkRoomDataBase.class,"work_database")
                                  .addMigrations(ALL_MIGRATIONS)
              ....
  • 注意,定义INTEGER字段的时候  需要加NOT NULL DEFAULT 0,否则汇报错(IlegalStateException)

猜你喜欢

转载自www.cnblogs.com/endian11/p/11950657.html