Android 有条件的房间数据库迁移

Android Room DB migration with condition

我在 Room DB 中错误地迁移了 table:

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 expected, got 'TABLE'”如何正确实现这个?

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

UPDATE RideEntity SET state = 1 where state = 0

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

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