Android 如何在文件室迁移语句中使用Kotlin字符串模板

Android 如何在文件室迁移语句中使用Kotlin字符串模板,android,kotlin,android-sqlite,android-room,Android,Kotlin,Android Sqlite,Android Room,我正在尝试创建房间迁移,如下所示:- class Migration_1_2(startVersion: Int = 1, endVersion: Int = 2) : Migration(startVersion, endVersion) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL(CREATE_TEMP_MY_TABLE_TABLE) data

我正在尝试创建房间迁移,如下所示:-

class Migration_1_2(startVersion: Int = 1, endVersion: Int = 2) : Migration(startVersion, endVersion) {

    override fun migrate(database: SupportSQLiteDatabase) {

        database.execSQL(CREATE_TEMP_MY_TABLE_TABLE)
        database.execSQL(COPY_MY_TABLE)
        database.execSQL(DROP_MY_TABLE_TABLE)
        database.execSQL(ALTER_TEMP_MY_TABLE_TABLE)

    }

    companion object {

        private const val NEW_COLUMN_A_EMPTY = " "
        private const val NEW_COLUMN_B_EMPTY = " "
        private const val NEW_COLUMN_C_EMPTY = " "
        private const val NEW_COLUMN_D_EMPTY = " "        

        private const val CREATE_TEMP_MY_TABLE_TABLE = "CREATE TABLE 'temp_myTable' ('column_one' TEXT, 'column_two' TEXT, 'column_three' TEXT, 'column_four' TEXT, 'uuid' TEXT NOT NULL, 'new_column_a' TEXT, 'column_five' TEXT, 'column_six' TEXT, 'new_column_b' TEXT, 'new_column_c' TEXT, 'new_column_d' TEXT, 'new_column_e' TEXT, PRIMARY KEY('uuid'))"
        private const val COPY_MY_TABLE = "INSERT INTO temp_myTable SELECT column_one, column_two, column_three, column_four, uuid, $NEW_COLUMN_A_EMPTY, column_five, column_six, $NEW_COLUMN_B_EMPTY, $NEW_COLUMN_C_EMPTY, $NEW_COLUMN_D_EMPTY, $NEW_COLUMN_E_EMPTY FROM myTable"
        private const val DROP_MY_TABLE_TABLE = "DROP TABLE 'myTable'"
        private const val ALTER_TEMP_MY_TABLE_TABLE = "ALTER TABLE 'temp_myTable' RENAME TO 'myTable'"

    }
}
使用
NEW_COULMN_#u EMPTY
字符串常量而不是“
”来记录将哪个列设置为
BLANK

即使Android Studio报告了
expected、get'、'

我猜这是
Room
不喜欢
Kotlin
字符串模板


有什么方法可以在我的sqlite原始SQL中使用
Kotlin
字符串模板吗?

您需要在字符串模板中使用Kotlin变量,对吗?您可以通过“${variable}”执行此操作,它将替换为字符串中的变量值。基本上,您要做的是在sql查询中插入和添加额外的空间。它不会被自动删除吗。?请尝试更改(在“”)->private const val NEW_COLUMN_35;_EMPTY=“”@AkhilSoman感谢您发现了这个令人尴尬的错误:(您需要在字符串模板中使用Kotlin变量,对吗?您可以通过“${variable}”完成此操作)这将替换为字符串中的变量值。基本上,您所做的是在sql查询中插入额外的空间。它不会被自动删除吗?您是否可以尝试更改(在“”中添加额外的“)->private const val NEW_COLUMN_35;#_EMPTY=“”@AkhilSoman感谢您发现这一点,这是我的一个尴尬错误:(