其他分享
首页 > 其他分享> > android-房间迁移:“没有这样的表:room_table_modification_log”

android-房间迁移:“没有这样的表:room_table_modification_log”

作者:互联网

Room 1.1.0版本.

迁移后的第一次运行时出现此错误.

如果我关闭应用程序并重新启动,它将运行良好.

ROOM: Cannot run invalidation tracker. Is the db closed?
java.lang.IllegalStateException: Cannot perform this operation because the connection pool has been closed.
    at android.database.sqlite.SQLiteConnectionPool.throwIfClosedLocked(SQLiteConnectionPool.java:1182)
    at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:792)
    at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:518)

Caused By : SQL(query) error or missing database.
    (no such table: room_table_modification_log (code 1): , while compiling: DELETE FROM proposals WHERE hotel_id = ?)
#################################################################
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1095)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:660)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1417)
    at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.compileStatement(FrameworkSQLiteDatabase.java:64)
    at android.arch.persistence.room.RoomDatabase.compileStatement(RoomDatabase.java:244)
    at android.arch.persistence.room.SharedSQLiteStatement.createNewStatement(SharedSQLiteStatement.java:65)
    at android.arch.persistence.room.SharedSQLiteStatement.getStmt(SharedSQLiteStatement.java:77)
    at android.arch.persistence.room.SharedSQLiteStatement.acquire(SharedSQLiteStatement.java:87)
    at com.hotellook.db.dao.FavoritesDao_Impl.removeProposals(FavoritesDao_Impl.java:723)

数据库初始化代码如下所示(Dagger AppModule)

@Provides
@Singleton
fun provideDataBase(app: Application): AppDatabase =
  Room.databaseBuilder(app, AppDatabase::class.java, DATABASE_NAME)
      .addMigrations(MIGRATION_1_2)
      .addMigrations(MIGRATION_2_3)
      .build()

private companion object {
  const val DATABASE_NAME = "app.db"

  val MIGRATION_1_2 = object : Migration(1, 2) {
    override fun migrate(database: SupportSQLiteDatabase) {
      database.execSQL("ALTER TABLE table1 ADD COLUMN column0 TEXT;")
      database.execSQL("ALTER TABLE table2 ADD COLUMN column0 TEXT;")
    }
  }

  val MIGRATION_2_3 = object : Migration(2, 3) {
    override fun migrate(database: SupportSQLiteDatabase) {
      database.execSQL("CREATE TABLE table0 (column1 INTEGER NOT NULL, column2 TEXT NOT NULL, PRIMARY KEY(column1))")
      database.execSQL("ALTER TABLE table1 ADD COLUMN column1 TEXT;")
      database.execSQL("ALTER TABLE table1 ADD COLUMN column2 TEXT;")
      database.execSQL("ALTER TABLE table1 ADD COLUMN column3 REAL;")
      database.execSQL("ALTER TABLE table2 ADD COLUMN column1 TEXT;")
      database.execSQL("ALTER TABLE table2 ADD COLUMN column2 REAL;")
      database.execSQL("ALTER TABLE table3 ADD COLUMN column1 INTEGER DEFAULT 0;")
      database.execSQL("ALTER TABLE table3 ADD COLUMN column2 TEXT;")
    }
  } 

该应用程序从版本2升级到版本3.
我该如何解决?

解决方法:

将版本升级到1.1.1-rc1,再也不会发生由Google自己的代码引起的这种无意义的错误,我已经在许多设备上测试了相同的场景.

另请参阅我的question/answer here

标签:android-room,android,sqlite,database-migration
来源: https://codeday.me/bug/20191010/1886913.html