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 12345@qq.com");
        }
    };


}

实体类中某一个字段不要了,删除某一列数据时:需要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");
        }
    };


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

javaGHui

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值