Room报错

 Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class XXX {
             ^
  Tried the following constructors but they failed to match:

.....
不要在构造函数中使用 @Ignore标签

@Entity(tableName = "product_match")
data class ProductMatch(
    @PrimaryKey(autoGenerate = true) val id: Long,
    //错误写法
    //@Ignore var isSelect:Boolean = false
    var appname: String? = null,
    var filter:Int = 0,
    var adverid: String? = null,
    var devicehead: String? = null,
    var productname_zh: String? = null,
    var productname_en: String? = null,
    var cidpid: String? = null,
    var producttype: String? = null,
    var icon: String? = null){
    @Ignore var isSelect:Boolean = false
}


Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
在build gradle中添加(推荐)

android {
    ...
    defaultConfig {
    ...
        javaCompileOptions {
            annotationProcessorOptions {
            arguments = ["room.schemaLocation":
            "$projectDir/schemas".toString()]
                }
            }
        }
}
 

java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: 570e1a71cb57b5b7c4fa32ab90636b20, found: 3ebd71e5059015352fc46579cf1a55d4
这个错误通常发生在使用 Room 数据库时,Room 检测到数据库的模式(schema)已更改,但版本号没有相应地递增

@Database(entities = [YourEntity::class], version = 2) // 递增版本号为2
abstract class YourAppDatabase : RoomDatabase() {
    // 数据访问对象(DAO)和其他数据库配置
}

java.lang.IllegalStateException: A migration from 1 to 2 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
这个错误表明 Room 数据库需要从版本 1 迁移到版本 2,但您的应用程序中未提供相应的数据库迁移。

//提供数据库迁移:
//在您的 Room 数据库配置中,为版本 1 到版本 2 的迁移创建一个迁移对象,并使用 addMigrations 方法将它添加到数据库构建器中。这样,Room 将能够执行必要的数据库迁移操作。
val MIGRATION_1_2: Migration = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
        // 执行从版本 1 到版本 2 的迁移操作
    }
}
Room.databaseBuilder(context, YourAppDatabase::class.java, "your_database_name")
    .addMigrations(MIGRATION_1_2) // 添加迁移对象
    .build()
//允许破坏性迁移:
//如果您不需要保留旧数据,或者迁移的逻辑非常复杂,您可以选择允许破坏性迁移。这将导致 Room 删除旧数据库并创建一个新的版本,而不是执行迁移。
Room.databaseBuilder(context, YourAppDatabase::class.java, "your_database_name")
    .fallbackToDestructiveMigration() // 允许破坏性迁移
    .build()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值