Android: Basic usage of Room database—Simple application_three_version

New fields added in entity class:

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

     public String getEmail() {
        return email;
    }

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

}

An error will be reported when compiling and starting again:

  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 needs to be modified to change the version number and strategy 

1. The version changes and the previous data is not retained 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. The version changes, retain the previous data 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]");
        }
    };


}

A certain field in the entity class is no longer needed. When deleting a certain column of data: 4 steps are required

//之前版本是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");
        }
    };


}

Guess you like

Origin blog.csdn.net/jiayou2020527/article/details/134928203