【问题标题】:Duplicate column name: <Column Name>(code 1 SQLITE_ERROR): What's wrong with Room?列名重复:<列名>(代码 1 SQLITE_ERROR):Room 出了什么问题?
【发布时间】:2022-08-10 20:42:42
【问题描述】:

我的应用程序中有一个SQLiteDataBase,包含三列。比我将它迁移到 Room 数据库如下:

@Database(entities = [Vehicle::class], version = 6, exportSchema = false)
abstract class VehicleDatabase : RoomDatabase() {

    abstract val dao: Dao

    companion object {

        @Volatile
        private var INSTANCE: VehicleDatabase? = null

        private val migration = object : Migration(1, 5) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.apply {
                    execSQL(\"CREATE TABLE vehicles_tmp (_id INTEGER, Manufacturer TEXT, Model TEXT, Created TEXT, Modified TEXT, PRIMARY KEY(_id))\")
                    execSQL(\"INSERT INTO vehicles_tmp (_id, Manufacturer, Model, Created, Modified) SELECT _id, Manufacturer, Model, Date, Date FROM Vehicles\")
                    execSQL(\"DROP TABLE Vehicles\")
                    execSQL(\"ALTER TABLE vehicles_tmp RENAME TO Vehicles\")
                    execSQL(\"ALTER TABLE Vehicles ADD COLUMN Tags TEXT\")
                }
            }
        }

        private val addColor = object : Migration(5, 6) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL(\"ALTER TABLE Vehicles ADD COLUMN Color TEXT\")
            }
        }

        fun getInstance(context: Context): VehicleDatabase = INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(
                context.applicationContext,
                VehicleDatabase::class.java,
                \"Vehicles.db\"
            )
                .addMigrations(migration, addColor)
                .build()
            INSTANCE = instance
            return instance
        }

    }
}

第一次迁移是从版本 1 到版本 5,如您所见,我又添加了一个列(颜色)。因此,我添加了从 5 到 6 的迁移。

一切正常,但现在我在 Firebase 中的 Crashlytics 出现了异常

Fatal Exception: android.database.sqlite.SQLiteException: duplicate column name: Color (code 1 SQLITE_ERROR): , while compiling: ALTER TABLE Vehicles ADD COLUMN Color TEXT
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1055)
       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:662)
       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
       at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
       at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1949)
       at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1871)
       at androidx.sqlite.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:265)
       at com.xxxxxxxx.xxxxxxxx.data.vehicles.NoteDatabase$Companion$addColor$1.migrate(VehicleDatabase.java:39)
       at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:99)
       at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:183)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:421)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:321)
       at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:151)
       at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:112)
       at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:706)
       at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:483)
       at androidx.room.RoomDatabase.query(RoomDatabase.java:526)
       at androidx.room.util.DBUtil.query(DBUtil.java:86)
       at com.xxxxxxxx.xxxxxxxx.data.vehicles.Dao_Impl$9.call(Dao_Impl.java:262)
       at com.xxxxxxxx.xxxxxxxx.data.vehicles.Dao_Impl$9.call(Dao_Impl.java:259)
       at androidx.room.CoroutinesRoom$Companion$createFlow$1$1$1.invokeSuspend(CoroutinesRoom.java:128)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:106)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
       at java.lang.Thread.run(Thread.java:923)

为什么它会抛出那个异常?似乎它试图在已经添加的情况下添加该列。

还是我做错了什么?

  • 猜测一下,您已更改 Vehicles Entity 类以合并新的颜色列,然后从全新安装(版本 5)运行,然后仅在运行后将版本号更改为 6。

标签: android android-room


【解决方案1】:

我面临同样的问题,Firebase Crashlytics 为一些用户报告了这个问题,因为我测试它我无法在我的最后重现

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-16
    • 1970-01-01
    • 2015-05-20
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    相关资源
    最近更新 更多