【问题标题】:Android Room DB migration with condition [closed]有条件的Android Room DB迁移[关闭]
【发布时间】:2021-06-13 10:00:36
【问题描述】:

我错误地迁移了 Room DB 中的表:

val MIGRATION_6_7 = object : Migration(6, 7) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE RideEntity ADD COLUMN state INTEGER DEFAULT 0 NOT NULL")
        }
    }

一些用户收到了值为 0 的新列。我应该做的是:

 val MIGRATION_6_7 = object : Migration(6, 7) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("ALTER TABLE RideEntity ADD COLUMN state INTEGER DEFAULT 1 NOT NULL")
        }
    }

我需要再做一次迁移,因为该列中有 0 的用户需要更改为 1。我正在添加此迁移:

   val MIGRATION_7_8 = object : Migration(7, 8) {
        override fun migrate(database: SupportSQLiteDatabase) {
            database.execSQL("UPDATE TABLE RideEntity SET state 1 where state 0")
        }
    }

但我得到“ or OR expected, got 'TABLE'”如何正确实现?

【问题讨论】:

    标签: android sql android-room


    【解决方案1】:

    您的更新语句语法不正确,应该是:

    UPDATE RideEntity SET state = 1 where state = 0
    

    您不应该有 TABLE 关键字,您需要使用 = 运算符来进行更新和条件。

    您可以查看here 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多