gpt4 book ai didi

android - 房间迁移-如何向现有表添加新的 NON-NULL 和 String 类型的列

转载 作者:行者123 更新时间:2023-12-05 00:19:08 35 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我在我的应用程序中实现了房间数据库。我的应用程序已经在 Playstore 上运行,因为现在我正在尝试将更新发送到模式发生变化的应用程序。我已经添加了新列,它的类型是字符串并且它应该是 NOT NULL 所以我将提供一个默认值。

我已经为此编写了一个迁移查询,但应用程序总是崩溃。

问题:为什么预期 modifyDate 列不是 Null=false,但我希望它不是 NULL。我没有看到 Expected 和 Found 部分有任何区别,期望 notNull 状态和值

我无法找到这次崩溃的真正原因。

下面是我的代码:

实体类

@Keep
@Entity
class PdfFile() {
//Columns before migration
@PrimaryKey(autoGenerate = true)
private var mid = 0
private var mFile_name: String? = null
private var mFile_size = "0mb"
private var mFileDate = "17-06-2020"
private var mParent_file = ""
private var mAbsolute_path: String? = null
private var fileType = ""
private var isViewed = false
private var isBookmarked = false

//TEST MIGRATION
@ColumnInfo(name = "modifyDate")
private var modifyDate = ""

fun setMid(mid: Int) {
this.mid = mid
}

fun getMid(): Int {
return mid
}

fun getMFile_name(): String? {
return mFile_name
}

fun getMFile_size(): String {
return mFile_size
}

fun getMFileDate(): String {
return mFileDate
}

fun getMParent_file(): String {
return mParent_file
}

fun getMAbsolute_path(): String? {
return mAbsolute_path
}

fun getFileType(): String {
return fileType
}

fun getIsViewed(): Boolean {
return isViewed
}

fun getIsBookmarked(): Boolean {
return isBookmarked
}

fun getModifyDate(): String {
return modifyDate
}

fun setMFile_name(name: String) {
this.mFile_name = name
}

fun setMFile_size(size: String) {
this.mFile_size = size
}

fun setMFileDate(date: String) {
this.mFileDate = date
}

fun setMParent_file(parentFile: String) {
this.mParent_file = parentFile
}

fun setMAbsolute_path(path: String) {
this.mAbsolute_path = path
}

fun setFileType(fileType: String) {
this.fileType = fileType
}

fun setIsViewed(isView: Boolean) {
this.isViewed = isView
}

fun setIsBookmarked(isBookmark: Boolean) {
this.isBookmarked = isBookmark
}

fun setModifyDate(date: String) {
this.modifyDate = date
}

override fun equals(obj: Any?): Boolean {
return if (obj !is PdfFile) {
false
} else {
val PdfFile = obj as PdfFile
mFile_name == PdfFile.getMFile_name()
&& mAbsolute_path == PdfFile.getMAbsolute_path()
&& mFile_size == PdfFile.getMFile_size()
}
}
}

房间数据库类

//Please note the previous version was 1 now i have changed to 2 after adding a new column to entity
@Database(
entities = [PdfFile::class],
version = 2,
exportSchema = false
)
abstract class RoomDbData : RoomDatabase() {
abstract fun taskDao(): DataDao


}

DatabaseManager 类

const val DATABASE_NAME = “MyDb”

class DatabaseManager(context: Context) {

//Migrating from version 1 to version 2
val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE PdfFile ADD COLUMN modifyDate TEXT NOT NULL DEFAULT '' ")
}
}

val appDatabase: RoomDbData

init {
appDatabase =
Room.databaseBuilder(context, RoomDbData::class.java, DATABASE_NAME)
.allowMainThreadQueries()
.addMigrations(MIGRATION_1_2)
.build()
}


}

异常(exception)情况

    Process: mypackagename, PID: 10993
java.lang.RuntimeException: Exception while computing database live data.
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:92)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.IllegalStateException: Migration didn't properly handle: PdfFile(mypackagename.models.entities.PdfFile).
Expected:
TableInfo{name='PdfFile', columns={isBookmarked=Column{name='isBookmarked', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, modifyDate=Column{name='modifyDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mFile_size=Column{name='mFile_size', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mid=Column{name='mid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, mFileDate=Column{name='mFileDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mAbsolute_path=Column{name='mAbsolute_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mParent_file=Column{name='mParent_file', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mFile_name=Column{name='mFile_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, fileType=Column{name='fileType', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, isViewed=Column{name='isViewed', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='PdfFile', columns={isBookmarked=Column{name='isBookmarked', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, modifyDate=Column{name='modifyDate', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue=''''}, mFile_size=Column{name='mFile_size', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mid=Column{name='mid', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, mFileDate=Column{name='mFileDate', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mAbsolute_path=Column{name='mAbsolute_path', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mParent_file=Column{name='mParent_file', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, mFile_name=Column{name='mFile_name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, fileType=Column{name='fileType', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, isViewed=Column{name='isViewed', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
at androidx.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:103)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:124)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:299)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:92)
at androidx.sqlite.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:53)
at androidx.room.RoomDatabase.inTransaction(RoomDatabase.java:476)
at androidx.room.RoomDatabase.assertNotSuspendingTransaction(RoomDatabase.java:281)
2020-10-20 15:14:41.031 10993-11039/mypackagename E/AndroidRuntime: at androidx.room.RoomDatabase.query(RoomDatabase.java:324)
at androidx.room.util.DBUtil.query(DBUtil.java:83)
at mypackagename.models.dao.DataDao_Impl$13.call(DataDao_Impl.java:325)
at mypackagename.models.dao.DataDao_Impl$13.call(DataDao_Impl.java:322)
at androidx.room.RoomTrackingLiveData$1.run(RoomTrackingLiveData.java:90)
... 3 more
2020-10-20 15:14:41.064 1764-1833/? E/InputDispatcher: channel 'f088a5d mypackagename/mypackagename.views.activities.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-10-20 15:14:41.067 1764-1833/? E/InputDispatcher: channel '1aca3d4 mypackagename/mypackagename.views.activities.SplashActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
2020-10-20 15:14:41.110 1764-4588/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-10-20 15:14:41.111 1764-4588/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
2020-10-20 15:14:41.111 1764-4588/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-10-20 15:14:41.111 1764-4588/? E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
2020-10-20 15:14:41.233 1541-4743/? E/AudioFlinger: not enough memory for AudioTrack size=131296
2020-10-20 15:14:41.233 1541-4743/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
2020-10-20 15:14:41.233 2486-7131/? E/AudioRecord: AudioFlinger could not create record track, status: -12
2020-10-20 15:14:41.236 2486-7131/? E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
2020-10-20 15:14:41.236 2486-7131/? E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
2020-10-20 15:14:41.237 2486-7131/? E/ActivityThread: Failed to find provider info for

请注意,我没有为此次迁移对 DAO 进行任何更改。那里也需要改变吗?

有人可以帮我解决这个问题吗?任何帮助将不胜感激。谢谢

最佳答案

要确保 modifyDate 不为空,您需要进行以下更改:

private var modifyDate = ""

到:

private var modifyDate: String = ""

关于android - 房间迁移-如何向现有表添加新的 NON-NULL 和 String 类型的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64438705/

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