gpt4 book ai didi

android - 将房间迁移到新版本时崩溃

转载 作者:行者123 更新时间:2023-11-29 14:12:25 24 4
gpt4 key购买 nike

我的数据库中的表配置文件版本为 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)

最佳答案

尝试使“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 test: String = ""var test: String? = 空终于成功了!

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

private String test = null

关于android - 将房间迁移到新版本时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51155042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com