[英]Room Migration didn't properly handle
得到以下異常:
預期:
TableInfo {name ='chat_table',列= {message = Column {name ='message',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},messageStatus = Column {name ='messageStatus ',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id',type ='INTEGER',affinity ='3',notNull = true,primaryKeyPosition = 1}},foreignKeys = [],索引= []}
實測:
TableInfo {name ='chat_table',column = {messageStatus = Column {name ='messageStatus',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0},id = Column {name ='id ',type ='INTEGER',affinity ='3',notNull = false,primaryKeyPosition = 1},message = Column {name ='message',type ='TEXT',affinity ='2',notNull = false,primaryKeyPosition = 0}},foreignKeys = [],索引= []}
任何人都可以提出這里的問題嗎? 我只是將表列值復制到另一個表。 然后刪除第一個表,然后將新表重命名為舊名稱。
1. database.execSQL("CREATE TABLE chat_table_new (id INTEGER,
messageStatus TEXT, message TEXT, PRIMARY KEY(id))")
2. database.execSQL("INSERT INTO chat_table_new SELECT * FROM
chat_table")
3. database.execSQL("DROP TABLE chat_table")
4. database.execSQL("ALTER TABLE chat_table_new RENAME TO chat_table")
嘗試這種方式..
@Database(entities = {Student.class, BookIssue.class},version = 1)
public abstract class StudentDatabase extends RoomDatabase {
private static StudentDatabase studentDatabase;
public static StudentDatabase getStudentDatabase(Context context)
{
if (studentDatabase==null)
{
studentDatabase= Room.databaseBuilder(context.getApplicationContext(),StudentDatabase.class,"student-database").addMigrations(StudentDatabase.MIGRATION_1_2).build();
}
return studentDatabase;
}
public abstract StudentDao studentDao();
public abstract BookDao bookDao();
public static final Migration MIGRATION_1_2 =new Migration(1,2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("alter table Student "+"add column pf String");
}
};
}
更多理解請參閱此鏈接https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.