AndroidX会议室数据库

时间:2018-07-23 12:36:07

标签: android kotlin android-room androidx

我通过androidX库在kotlin中使用会议室数据库。当我下载feed并尝试保存在数据库中时,崩溃发生,并显示错误sqlite:NOTNULL约束失败。JsonFeed在某些标签中包含对象,在某些标签中也包含空数据。

但是我没有在数据库中添加任何非null属性。在另一个项目iam中,它使用Java的Arch库中的房间数据库以相同的feed进行工作。

我尝试了一些seconday构造函数

constructor(0, "", "", "", "", listOf<SubMenuApi>, 0)

还有这个...

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import androidx.room.TypeConverters

@Entity(tableName = "tableMenu")
data class MenuApi(

        @PrimaryKey(autoGenerate = true) var id: Int = 0,
        @ColumnInfo(name = "title") @SerializedName("Title") var title: String = "",
        @ColumnInfo(name = "TITLE_OF_ACCESS") @SerializedName("TitleofAccess") var titleOfAccess: String = "",
        @ColumnInfo(name = "apexFileName") @SerializedName("AspxFileName") var apexFileName: String = "",
        @ColumnInfo(name = "sectionId") @SerializedName("SectionId") var sectionId: String = "",
        @TypeConverters(MenuConvertors::class) @SerializedName("SubMenu") var subMenuApi: List<SubMenuApi> = listOf<SubMenuApi>(),
        @ColumnInfo(name = "subSecCount") @SerializedName("subsec_count") var subSecCount: Int = 0)

我的数据库类

@Database(entities = [(MenuApi::class),], version = 1, exportSchema = false)
@TypeConverters(MenuConvertors::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun dbDao(): DbDao
}

菜单转换器

class MenuConvertors {

    private val gson = Gson()

    @TypeConverter
    fun stringToList(data: String?): List<SubMenuApi> {
        if (data == null) {
            return emptyList()
        }
        val listType = object : TypeToken<List<SubMenuApi>>() {}.type
        return gson.fromJson(data, listType)
    }

    @TypeConverter
    fun listToString(subMenuApiList: List<SubMenuApi>): String {
        return gson.toJson(subMenuApiList)
    }
}

子菜单

data class SubMenuApi(

        @SerializedName("Title") var title: String? = "",
        @SerializedName("TitleofAccess") var titleOfAccess: String? = "",
        @SerializedName("AspxFileName") var apexFileName: String? = "",
        @SerializedName("SectionId") var sectionId: String? = "",
        @SerializedName("URL") var url: String? = "")

我的错误是

android.database.sqlite.SQLiteConstraintException: NOT NULL constraint failed: tableMenu.apexFileName (code 1299)
    #################################################################
    Error Code : 1299 (SQLITE_CONSTRAINT_NOTNULL)
    Caused By : Abort due to constraint violation.
        (NOT NULL constraint failed: tableMenu.apexFileName (code 1299))
    #################################################################
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:915)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at androidx.sqlite.db.framework.FrameworkSQLiteStatement.executeInsert(FrameworkSQLiteStatement.java:51)
        at androidx.room.EntityInsertionAdapter.insert(EntityInsertionAdapter.java:97)
        at com.thanthi.dtnext.dtnextapplication.database.DbDao_Impl.insertMenuData(DbDao_Impl.java:432)
        at com.thanthi.dtnext.dtnextapplication.viewmodel.MenuViewModel$loadMenuData$1.onResponse(MenuViewModel.kt:53)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7325)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

apexFileName中的空标记

我的代码是否有错误或androidX.room中的错误

3 个答案:

答案 0 :(得分:1)

由于您使用的是Kotlin,因此如果变量可以为null,则它们必须是可变的。

在您实体中的变量类型之后添加?,它将对其进行修复。如果没有?,则基本上是在保证它不会为空。

@PrimaryKey(autoGenerate = true) var id: Int? = 0, @ColumnInfo(name = "title")
@SerializedName("Title") var title: String? = "", 
@ColumnInfo(name = "TITLE_OF_ACCESS") @SerializedName("TitleofAccess") var titleOfAccess: String? = "", 
@ColumnInfo(name = "apexFileName") @SerializedName("AspxFileName") var apexFileName: String? = "", 
@ColumnInfo(name = "sectionId") @SerializedName("SectionId") var sectionId: String? = "", 
@TypeConverters(MenuConvertors::class) @SerializedName("SubMenu") var subMenuApi: List<SubMenuApi>? = listOf<SubMenuApi>(), 
@ColumnInfo(name = "subSecCount") @SerializedName("subsec_count") var subSecCount: Int? = 0)

答案 1 :(得分:1)

在字符串中添加null后,初始化代码可以正常工作..像这样

@ColumnInfo(name = "title") @SerializedName("Title") var title: String? = null

当我们使用空值初始化变量时,会议室数据库将表中的该列标记为NOT NULL,因此当我们将数据接收为null时,它将出错。

因此,当我们不知道值时,必须在kotlin数据类中将变量初始化为null

答案 2 :(得分:0)

创建后是否更改了数据库方案? =>卸载应用程序并重建

此外,您还应该设置exportSchema = true,以便实际上看到生成的模式。