今天主要学习了数据库的迁移。
1、原entity的修改
原先的entity可以直接修改,例如原来的为:
package com.example.roombasic; ? import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; ? @Entity public class Word { @PrimaryKey(autoGenerate = true) private int id; ? @ColumnInfo(name = "english_word") private String word; ? @ColumnInfo(name = "chinese_mean") private String chineseMean; ? public Word(String word, String chineseMean) { this.word = word; this.chineseMean = chineseMean; } ? public int getId() { return id; } ? public void setId(int id) { this.id = id; } ? public String getWord() { return word; } ? public void setWord(String word) { this.word = word; } ? public String getChineseMean() { return chineseMean; } ? public void setChineseMean(String chineseMean) { this.chineseMean = chineseMean; } }
可以直接修改为:
package com.example.roombasic; ? import androidx.room.ColumnInfo; import androidx.room.Entity; import androidx.room.PrimaryKey; ? @Entity public class Word { @PrimaryKey(autoGenerate = true) private int id; ? @ColumnInfo(name = "english_word") private String word; ? @ColumnInfo(name = "chinese_mean") private String chineseMean; @ColumnInfo(name = "is_ok") private boolean isOk; ? public boolean isOk() { return isOk; } ? public void setOk(boolean ok) { isOk = ok; } ? public Word(String word, String chineseMean) { this.word = word; this.chineseMean = chineseMean; } ? public int getId() { return id; } ? public void setId(int id) { this.id = id; } ? public String getWord() { return word; } ? public void setWord(String word) { this.word = word; } ? public String getChineseMean() { return chineseMean; } ? public void setChineseMean(String chineseMean) { this.chineseMean = chineseMean; } }
新添加的属性记得添加get和set函数。
2、database的修改
1、不保留原有数据的迁移
不保留原有数据的修改,可以直接将数据库类的版本修改为原版本+1,然后构建数据库类的时候添加一个.fallbackToDestructiveMigration()。
package com.example.roombasic; ? import android.content.Context; ? import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; @Database(entities = {Word.class},version = 2,exportSchema = false) public abstract class WordDatabase extends RoomDatabase { private static WordDatabase INSTANCE; static synchronized WordDatabase getDatabase(Context context){ if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database") .fallbackToDestructiveMigration() .build(); } return INSTANCE; } ? public abstract WordDao getWordDao(); }
2、保留数据的迁移
此时需要使用另一个方法.addMigrations(),并添加自定义的迁移方式,如:
package com.example.roombasic; ? import android.content.Context; ? import androidx.annotation.NonNull; import androidx.room.Database; import androidx.room.Room; import androidx.room.RoomDatabase; import androidx.room.migration.Migration; import androidx.sqlite.db.SupportSQLiteDatabase; ? @Database(entities = {Word.class},version = 2,exportSchema = false) public abstract class WordDatabase extends RoomDatabase { private static WordDatabase INSTANCE; static synchronized WordDatabase getDatabase(Context context){ if (INSTANCE == null) { INSTANCE = Room.databaseBuilder(context.getApplicationContext(),WordDatabase.class,"word_database") .addMigrations() .build(); } return INSTANCE; } ? public abstract WordDao getWordDao(); ? private static final Migration MIGRATION_1_2 = new Migration(1,2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE word ADD COLUMN is_ok INTEGER NOT NULL DEFAULT 0"); } }; }
-
Migration(1,2) 中的两个参数是迁移的两个版本号
-
ALTER TABLE word ADD COLUMN is_ok INTEGER NOT NULL DEFAULT 0
-
是增加一列,名称为is_ok,不能为空,默认值为0。
-
sqlite中没有boolean中,用int代替。
-