android - Kotlin andoid 预填充数据库
问题描述
我正在尝试构建一个非常简单的数据库来存储 Hangman 游戏的分数。
我建立了一个实体:
@Entity(tableName = "scores") 数据类 DBScore(@PrimaryKey var pseudo: String = "", var score: Int = 0) {
}
//从DB模型到kotlin模型的转换方法 fun List.asDomainModel(): List { return map { Score( pseudo = it.pseudo, score = it.score ) }
}
使用 db 文件用样本值预填充 db
INSTANCE = databaseBuilder( context.applicationContext, AppDataBase::class.java, "app_database" ) .createFromAsset("database/scores.db").build()
为此,我创建了以下数据库文件:https ://wetransfer.com/downloads/c534dd62136c94f17c0ee71f1ef39a1920201011144837/6ff2b7
但我收到一个错误,不明白我的 db 有什么问题:
原因:java.lang.IllegalStateException:预打包的数据库具有无效的架构:scores(com.example.hangmangame.database.entity.DBScore)。预期:TableInfo{name='scores',columns={pseudo=Column{name='pseudo',type='TEXT',affinity='2',notNull=true,primaryKeyPosition=1,defaultValue='null'}, score=Column{name='score', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} 找到: TableInfo{name='scores', columns={score=Column{name='score', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, pseudo=列{name='pseudo',type='TEXT',affinity='2',notNull=true,primaryKeyPosition=1,defaultValue='null'
似乎是列顺序的问题,但对我来说 SQL 对它不敏感。
感谢您的帮助。
问候
昆汀。
解决方案
房间预计score
是NOT NULL
。NULL
显然,基于错误,您预先填充的数据库架构将其设置为。
推荐阅读
- windows - Windows上的GCC编译到库?
- excel - 在循环中输入不匹配错误,在列中递增地添加数字
- java - 无法从具有 setView 的 AlertDialog 中的 EditText 获取文本
- api - 如何在 Flutter 中使用 CoinMarketCap API
- react-native - 不变违规:本机模块不能在开玩笑反应本机中为空
- intellij-idea - 如何在intellij中显示原始文件结构?
- java - 是否有可能在 Selenium 中刷新某个帧
- pandas - FinViz - 股票抓取给出错误 - AMZN 未找到 'NoneType' 对象没有属性 'find_next'
- r - r - 根据另一个 DF 的列中的值选择 DF
- python - 在 Django 中设置 PasswordResetForm 的语言?