室:java.lang.IllegalStateException:现有数据库的迁移不正确

时间:2018-06-24 13:28:20

标签: android android-sqlite android-room sqliteopenhelper

使用的房间版本:-1.1.1-rc1

我有一个由ormlite实现的现有数据库。我正在尝试使用ormlite生成的现有sqlite文件迁移到Room。

当我们在ormlite中为表列使用long数据类型时,在sqlite中它会隐蔽为BIGINT。因此创建的架构包含BIGINT类型的列,该列是room的未知类型。当我尝试使用Room升级应用程序时,出现迁移异常。

java.lang.IllegalStateException:迁移未正确

Process: com.sample.playground, PID: 5587
java.lang.IllegalStateException: Migration didn't properly handle SampleReports(com.sample.playground.model.SampleReport
 Expected:
TableInfo{name='SampleReports', columns={timeslot=Column{name='timeslot', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, imeisvSvn=Column{name='imeisvSvn', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='SampleReports', columns={environment=Column{name='environment', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0}, timeslot=Column{name='timeslot', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, trigger=Column{name='trigger', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0}, timestamp=Column{name='timestamp', type='BIGINT', affinity='3', notNull=true, primaryKeyPosition=0}, _id=Column{name='_id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}}, foreignKeys=[], indices=[]}
    at com.sample.playground.model.SampleReport.SampleReportRoomDbHelper_Impl$1.validateMigration(SampleReportRoomDbHelper_Impl.java:77)
    at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:87)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:133)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
    at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
    at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
    at com.sample.playground.model.SampleReportDao_Impl.getReports(SampleReportDao_Impl.java:300)

使用BigInteger尝试使用TypeConverter仍然无效。

2 个答案:

答案 0 :(得分:0)

我认为您需要通过覆盖 migrate 方法来相应地转换表,以便 -根据预期的列创建一个中间表, -将数据复制到中间表, -删除原始表格并 -然后将中间表的名称更改为原始名称

例如基于:-

DROP TABLE IF EXISTS SampleReports_intermediate;
CREATE TABLE IF NOT EXISTS SampleReports_intermediate (
    timeslot INTEGER NOT NULL, 
    environment TEXT NOT NULL,
    timestamp INTEGER NOT NULL,
    `trigger` INTEGER NOT NULL,
    imeisvSvn TEXT,
    _id INTEGER PRIMARY KEY
);
INSERT INTO SampleReports_intermediate (environment, timeslot, `trigger`, timestamp, _id)
    SELECT environment, timeslot,`trigger`, timestamp, _id FROM SampleReports
;
DROP TABLE IF EXISTS SampleReports;
ALTER TABLE SampleReports_intermediate RENAME TO SampleReports;

如果您不使用房间1.1,请尝试使用它,因为它具有:-

  

Room 1.1添加了对不在Room中的Sqlite类型的支持

Room Database Migration didn't properly handle conversion

答案 1 :(得分:0)

查看此答案https://stackoverflow.com/a/52127819/7433710 由于数据类型不匹配或最旧的Sqlite表中未给出最常见的Integer not null导致此错误,并且您尝试通过添加int变量进行初始化。确保确保在所有int列中添加NOT NULL。

使用下面的链接检查使用logcat输出得到的差异。没有足够的声誉来发布图像:(

Comparison using your logcat output

Full Image of the difference