【发布时间】:2021-05-26 19:07:21
【问题描述】:
我正在尝试在我的 Android 应用程序中实现 Room,但出现错误
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=[]}
我确实看到uts 和mid 列已切换,但是我无法弄清楚如何更改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>
}
如果这是我遇到的问题,谁能帮我对列进行排序?
【问题讨论】:
标签: android sqlite kotlin android-room