Android : Room 数据库的基本用法 —简单应用_三_版本

在实体类中添加了新字段:

@Entity(tableName = "people")
public class People {
     //新添加的字段
    private String email;

     public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

再次编译启动时会报错:

  java.lang.RuntimeException: Exception while computing database live data.
Caused by: java.lang.IllegalStateException: 
Room cannot verify the data integrity. 
Looks like you've changed schema but forgot to update the version number.
 You can simply fix this by increasing the version number. 
Expected identity hash: e24ea0779cf1452b08cb8aa058d3f37e, found: 2439a6f43e94aacbdd70254b0a25f90f
                                                                                                    

需要修改PeopleDataBase.java 更改版本号 和 策略 

1.版本发生改变 不保留之前的数据 fallbackToDestructiveMigration()

//之前版本是1 手动修改为2
@Database(entities = {People.class}, version = 2, exportSchema = false)
public abstract class PeopleDataBase extends RoomDatabase {

    private static PeopleDataBase peopleDataBase;
    public static synchronized PeopleDataBase getPeopleDataBase(Context context){
        if(peopleDataBase == null){
            peopleDataBase = Room.databaseBuilder(context.getApplicationContext(),PeopleDataBase.class,"peopleDB")
                  .fallbackToDestructiveMigration()//1.版本发生改变 不保留之前的数据
                  .build();
        }
        return peopleDataBase;
    }

   
    public abstract  PeopleDao peopleDao();
}

2. 版本发生改变 保留之前的数据    addMigrations(MIGRATION_1_2)

//之前版本是1 手动修改为2
@Database(entities = {People.class}, version = 2, exportSchema = false)
public abstract class PeopleDataBase extends RoomDatabase {

    private static PeopleDataBase peopleDataBase;
    public static synchronized PeopleDataBase getPeopleDataBase(Context context){
        if(peopleDataBase == null){
            peopleDataBase = Room.databaseBuilder(context.getApplicationContext(),PeopleDataBase.class,"peopleDB")
                   .addMigrations(MIGRATION_1_2)  //2.版本发生改变 会保留之前的数据
                  .build();
        }
        return peopleDataBase;
    }

   
    public abstract  PeopleDao peopleDao();

    //版本迁移策略
    static final Migration MIGRATION_1_2 = new Migration(1,2) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //手动添加字段
            database.execSQL("alter table people add column email text default [email protected]");
        }
    };


}

实体类中某一个字段不要了,删除某一列数据时:需要4步骤

//之前版本是1 手动修改为2
@Database(entities = {People.class}, version = 3, exportSchema = false)
public abstract class PeopleDataBase extends RoomDatabase {

    private static PeopleDataBase peopleDataBase;
    public static synchronized PeopleDataBase getPeopleDataBase(Context context){
        if(peopleDataBase == null){
            peopleDataBase = Room.databaseBuilder(context.getApplicationContext(),PeopleDataBase.class,"peopleDB")
                   .addMigrations(MIGRATION_2_3)  //2.版本发生改变 会保留之前的数据
                  .build();
        }
        return peopleDataBase;
    }

   
    public abstract  PeopleDao peopleDao();

    //版本迁移策略 删除某一列
    static final Migration MIGRATION_2_3 = new Migration(2,3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {
            //1.创建临时表
            database.execSQL("create table people_temp ( id Integer primary key not null , user_name text , age integer,sex text)");
            //2.往临时表中插入数据
            database.execSQL("insert into people_temp ( id, user_name, age, sex) select id,user_name,age,sex from people ");
            //3.删除people表
            database.execSQL("drop table people");
            //4.更改表名字,把临时表修改成people表
            database.execSQL("alter table people_temp rename to people");
        }
    };


}

猜你喜欢

转载自blog.csdn.net/jiayou2020527/article/details/134928203