[英]Database Migration from sqlite to room expected table handling
我不斷收到此錯誤,但我仍然不知道如何解決。 請幫幫我。 這是我的錯誤日志:
java.lang.RuntimeException: Exception while computing database live data.
Caused by: java.lang.IllegalStateException: Migration didn't properly handle: task(com.examples.TaskEntry).
Expected:
TableInfo{name='task', columns={description=Column{name='description', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, priority=Column{name='priority', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, updated_at=Column{name='updated_at', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='task', columns={}, foreignKeys=[], indices=[]}
下面是我的 TaskEntry 實體類,我認為所有問題都源於:
@Entity(tableName = "task")
public class TaskEntry {
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "description")
@NonNull
private String description;
@ColumnInfo(name = "priority")
private int priority;
@ColumnInfo(name = "updated_at")
private Date updatedAt;
@Ignore
public TaskEntry(@NonNull String description, int priority, Date updatedAt) {
this.description = description;
this.priority = priority;
this.updatedAt = updatedAt;
}
public TaskEntry(int id, @NonNull String description, int priority, Date updatedAt) {
this.id = id;
this.description = description;
this.priority = priority;
this.updatedAt = updatedAt;
}
在這段代碼中,我設法將遷移實例調用到我的房間數據庫類:
final static Migration MIGRATION_5_6 = new Migration(5, 6) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
}
};
public static AppDatabase getInstance(Context context) {
if (sInstance == null) {
synchronized (LOCK) {
Log.d(LOG_TAG, "Creating new database instance");
sInstance = Room.databaseBuilder(context.getApplicationContext(),
AppDatabase.class, AppDatabase.DATABASE_NAME)
.addMigrations(MIGRATION_5_6)
.addCallback(roomCallback)
.build();
}
}
Log.d(LOG_TAG, "Getting the database instance");
簡而言之,Room 找到了一個沒有任務表的數據庫,因此沒有列、外鍵或索引。
下面是我的 TaskEntry 實體類,我認為所有問題都源於:
這只會是問題,我相信,如果數據庫中的表名不是任務,例如,如果表被命名為任務並且使用@Entity(tableName = "task")
而不是 @Entity(tableName = "tasks") `
否則,問題似乎可能出在遷移中。 也許通過說/ 因為我們沒有改變表格,所以這里沒有其他事情可做。 實際發生的是您正在引入該表(否則,如果不添加或引入該表,為什么會有遷移)。 如果是這種情況,那么遷移應該是:-
final static Migration MIGRATION_5_6 = new Migration(5, 6) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// Since we didn't alter the table, there's nothing else to do here.
_db.execSQL("CREATE TABLE IF NOT EXISTS `task` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `description` TEXT NOT NULL, `priority` INTEGER NOT NULL, `updated_at` TEXT)");
}
};
但是,您可能會收到錯誤消息:-
error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
因此,您需要將updateAt列更改為String類型或編寫並實現 TypeConverter。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.