简体   繁体   English

Android Room - 迁移新表失败,出现错误“表已存在”

[英]Android Room - Migration of new table fails with error "Table already exists"

Crashlytics notify about an error with migration on the production version of my app. Crashlytics 通知我的应用程序生产版本的迁移错误。

Fatal Exception: android.database.sqlite.SQLiteException: table NotificationsRoomEntity already exists (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE NotificationsRoomEntity (
                id INTEGER NOT NULL,
                sentDate TEXT NOT NULL,
                title TEXT NOT NULL,
                message TEXT NOT NULL,
                type TEXT NOT NULL,
                commandId INTEGER NOT NULL,
                readDate TEXT,
                PRIMARY KEY(id)
            )
   at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
   at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:903)
   at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:514)
   at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)

This table is new in my app and cannot exist in the past.该表是我的应用程序中的新表,过去不能存在。 What circumstances are the reason for these exceptions?这些例外的原因是什么? The migration tests are running successfully, and there was no table with this name in the previous schemas.迁移测试运行成功,并且在之前的架构中没有具有此名称的表。 In some sources I have seen the " CREATE TABLE IF NOT EXISTS " procedure, but can I be sure that the table will be up-to-date if it really already exists?在某些来源中,我看到了“ CREATE TABLE IF NOT EXISTS ”过程,但是如果表确实已经存在,我可以确定它是最新的吗? My migration code is below.我的迁移代码如下。

val MIGRATION_6_7 = object : Migration(6, 7) {

override fun migrate(database: SupportSQLiteDatabase) {
    database.execSQL(
        """
            CREATE TABLE NotificationsRoomEntity (
                id INTEGER NOT NULL,
                sentDate TEXT NOT NULL,
                title TEXT NOT NULL,
                message TEXT NOT NULL,
                type TEXT NOT NULL,
                commandId INTEGER NOT NULL,
                readDate TEXT,
                PRIMARY KEY(id)
            )
        """
    )
}

} }

I have seen the "CREATE TABLE IF NOT EXISTS" procedure, but can I be sure that the table will be up-to-date if it really already exists?我已经看到“如果不存在则创建表”过程,但是如果表确实已经存在,我可以确定它是最新的吗? My migration code is below.我的迁移代码如下。

You can use:-您可以使用:-

val MIGRATION_6_7 = object : Migration(6, 7) {

    override fun migrate(database: SupportSQLiteDatabase) {
    database.execSQL("DROP TABLE IF EXISTS NotificationsRoomEntity;")
    database.execSQL(
        "
            CREATE TABLE IF NOT EXISTS NotificationsRoomEntity (
                id INTEGER NOT NULL,
                sentDate TEXT NOT NULL,
                title TEXT NOT NULL,
                message TEXT NOT NULL,
                type TEXT NOT NULL,
                commandId INTEGER NOT NULL,
                readDate TEXT,
                PRIMARY KEY(id)
            );
        "
    )
    }
}

Then you will know that the table will be correct.然后你就会知道表格是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM