将会议室迁移到新版本时崩溃

时间:2018-07-03 12:47:52

标签: android android-sqlite android-room

我的数据库具有表Profile(版本1)。现在,我想更改该表并添加文本字段createdAt。

我已经创建了迁移:

public static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override public void migrate(@NonNull SupportSQLiteDatabase database) {
  database.execSQL("ALTER TABLE Profile "+ " ADD COLUMN createdAt TEXT");
}
};

我在这里添加了它:

 @Provides @Singleton public AppDatabase  provideDatabase(@Named(DIConstants.APP) Context context) {
 return Room.databaseBuilder(context, AppDatabase.class, "appdb")
    .addMigrations(AppDatabase.MIGRATION_1_2)
    .build();
  }

现在,当我运行应用程序时,出现此错误:

Process: org.app.app.debug, PID: 4800
              java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/user/0/org.app.rapp.debug/databases/appdb
                  at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
                  at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1659)
                  at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
                  at android.arch.persistence.db.framework.FrameworkSQLiteDatabase.execSQL(FrameworkSQLiteDatabase.java:240)
                  at org.app.app.data.sql.AppDatabase$1.migrate(AppDatabase.java:65)
                  at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:73)
                  at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:118)
                  at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
                  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                  at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)
                  at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
                  at android.arch.persistence.room.RoomDatabase.inTransaction(RoomDatabase.java:305)
                  at android.arch.persistence.room.InvalidationTracker$1.run(InvalidationTracker.java:281)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:-1)

尝试使“ createdAt”为空。这对我有用。

@Entity(tableName = "RSyncStatus")
data class RSyncStatus(@PrimaryKey(autoGenerate = true)
                       var id: Long? = null,
                       var userObjId: String = "",
                       var syncStatus: Int? = null,
                       var test: String? = null)

我正在使用 kotlin 。所以我确实从var testString = ""var testString? = null 终于成功了!

我认为在Java中它是这样的:

private String test = null