预打包数据库的架构列顺序无效 [英] Pre-packaged database has an invalid schema column order

查看:27
本文介绍了预打包数据库的架构列顺序无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用程序中实现 Room,但出现错误

I am attempting to implement Room in my Android application, but am getting the error

java.lang.IllegalStateException: Pre-packaged database has an invalid schema: viewLog(myApp.ViewLogModel).
 
Expected:
TableInfo{name='viewLog', columns={vid=Column{name='vid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, mid=Column{name='mid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, uts=Column{name='uts', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 
Found:
TableInfo{name='viewLog', columns={VID=Column{name='VID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, UTS=Column{name='UTS', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, MID=Column{name='MID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}

我确实看到 utsmid 列被切换,但是我无法弄清楚如何更改 TableInfo<中的列顺序/代码>.

I do see the uts and mid columns are switched, however I have not been able to figure out how to change the column order in the TableInfo.

我的房间数据库代码是

@Database(entities = [ViewLogModel::class, CacheInputModel::class, CachePageModel::class], version = 1)
    abstract class MyDatabase : RoomDatabase() {
    abstract fun cacheInputDao(): CacheInputDao
    abstract fun cachePageDao(): CachePageDao
    abstract fun viewLogDao(): ViewLogDao

    companion object {
        // For Singleton Instance
        @Volatile
        private var INSTANCE: MyDatabase? = null

        fun getAppDataBase(context: Context): MyDatabase {
            return INSTANCE ?: synchronized(this) {
                INSTANCE ?: Room.databaseBuilder(context.applicationContext, MyDatabase::class.java, "appDatabase")
                    .createFromAsset("app.db")
                    .build()
            }
        }

        fun destroyDataBase(){
            INSTANCE = null
        }
    }
}

ViewLogModel

@Entity(tableName = "viewLog")
data class ViewLogModel(
    @PrimaryKey val mid: Int,
    val vid: Int,
    val uts: Float)

ViewLogDao

@Dao
interface ViewLogDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertOrUpdateViewLog(viewLog: ViewLogModel)

    @Update
    suspend fun updateViewLog(viewLog: ViewLogModel)

    @Delete
    suspend fun deleteAllViewLog(viewLog: ViewLogModel)

    @Query("DELETE FROM viewlog WHERE VID= :vid OR UTS < :uts")
    suspend fun deleteViewLog(vid: Int, uts: Float)

    @Query("SELECT * FROM viewLog")
    suspend fun getAll(): List<ViewLogModel>

    @Query("SELECT * FROM viewLog WHERE vid = :vid")
    suspend fun getViewLog(vid: Int): List<ViewLogModel>
}

如果这是我遇到的问题,有人可以帮我订购列吗?

Can anyone help me with ordering the columns if that is the issue that I'm encountering?

推荐答案

我相信您的问题不在于顺序,而在于列名的大小写.

I believe that your issue is not the order but rather the case of the column names.

例如以下数据库(另存为资产 appcase.db)定义为:-

For example the following database (save as asset appcase.db) defined with :-

CREATE TABLE "viewLog" (
  "MID" INTEGER NOT NULL,
  "UTS" real NOT NULL,
  "VID" INTEGER NOT NULL,
  PRIMARY KEY ("MID")
);

失败:-

 Caused by: java.lang.IllegalStateException: Pre-packaged database has an invalid schema: viewLog(a.a.so67711264kotlinroomprepackageddb.ViewLogModel).
 Expected:
TableInfo{name='viewLog', columns={vid=Column{name='vid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, mid=Column{name='mid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, uts=Column{name='uts', type='REAL', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
 Found:
TableInfo{name='viewLog', columns={VID=Column{name='VID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, UTS=Column{name='UTS', type='real', affinity='4', notNull=true, primaryKeyPosition=0, defaultValue='null'}, MID=Column{name='MID', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}

但是,使用 DDL 的数据库是:-

However, using a database who's DDL is:-

CREATE TABLE "viewLog" (
  "mid" INTEGER NOT NULL,
  "uts" real NOT NULL,
  "vid" INTEGER NOT NULL,
  PRIMARY KEY ("mid")
)

按预期工作.

因此,您需要像上面一样更改 DDL 或更改实体以反映 DDL,例如以下适用于原始(失败的)DDL:-

So you either need to change the DDL as above or change the Entity to reflect the DDL e.g. the following works with the original (failed) DDL:-

@Entity(tableName = "viewLog")
data class ViewLogModel(
    @PrimaryKey val MID: Int,
    val VID: Int,
    val UTS: Float)

或者你可以使用 @ColumnInfo 之类的:-

or alternately you could use @ColumnInfo's like:-

@Entity(tableName = "viewLog")
data class ViewLogModel(
    @PrimaryKey @ColumnInfo(name = "MID") val mid: Int,
    @ColumnInfo(name ="VID") val vid: Int,
    @ColumnInfo(name = "UTS") val uts: Float)

这篇关于预打包数据库的架构列顺序无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆