简体   繁体   English

从 sqlite 到房间的数据库迁移预期表处理

[英]Database Migration from sqlite to room expected table handling

I keep getting this error and I still don't know how to solve it.我不断收到此错误,但我仍然不知道如何解决。 Please help me out here.请帮帮我。 This are my error Logs :这是我的错误日志:

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=[]}

Below is my TaskEntry Entity Class where I presume all the problem originates from:下面是我的 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;
    }

In this code I manage to call the Migration instance to my Room Database Class:在这段代码中,我设法将迁移实例调用到我的房间数据库类:

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");

In short, Room has found a database that has no task table, hence there being no columns, foreign keys or indicies.简而言之,Room 找到了一个没有任务表的数据库,因此没有列、外键或索引。

Below is my TaskEntry Entity Class where I presume all the problem originates from:下面是我的 TaskEntry 实体类,我认为所有问题都源于:

This would only be the issue, I beleive, if the table name in the database is not task eg if the table were named tasks and @Entity(tableName = "task") were used instead of @Entity(tableName = "tasks")`这只会是问题,我相信,如果数据库中的表名不是任务,例如,如果表被命名为任务并且使用@Entity(tableName = "task")而不是 @Entity(tableName = "tasks") `

Otherwise the issue would appear to perhaps be in the Migration.否则,问题似乎可能出在迁移中。 Perhaps by saying / Since we didn't alter the table, there's nothing else to do here.也许通过说/ 因为我们没有改变表格,所以这里没有其他事情可做。 What is actually happening is that you are introducing the table (otherwsie if not adding or introducing the table why would there be a migration).实际发生的是您正在引入该表(否则,如果不添加或引入该表,为什么会有迁移)。 If that were the case then the Migration should be :-如果是这种情况,那么迁移应该是:-

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)");
    }
};
  • Note the SQL has been copied from the createAllTables method in the AppDatabase_Impl.java in the generated Java after compiling (but not running the App).注意SQL是从编译后生成的Java中AppDatabase_Impl.javacreateAllTables方法中复制过来的(但不是运行App)。

However, you would likely then get an error :-但是,您可能会收到错误消息:-

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.

So you would need to eitherr change the updateAt column to be a type of String or Write and Implement a TypeConverter.因此,您需要将updateAt列更改为String类型或编写并实现 TypeConverter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM