如何检查房间数据库中是否插入了数据 [英] How to check if data is inserted in Room Database

查看:127
本文介绍了如何检查房间数据库中是否插入了数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我项目中新的Architecture组件中的Room Database. 我正在通过dao添加一些数据,但是在尝试检索它时却没有.您能建议我如何检查插入是否成功吗?以下是可帮助您理解问题的代码.

I am using Room Database from the new Architecture components in my project. I am adding some data through dao, but when trying to retrieve it I am not getting it. Can you please suggest me how to check whether the insert was successful or not? Below are the codes to help you understand the problem.

添加到数据库后,我与调试器进行了检查,该语句已成功执行.

adding to database, I checked with debugger, this statement executed successfully.

appContext.db.rallyDAO().addVehicleListItem(vehicle)

在插入后从此语句的数据库中获取空值.

Getting null from database on this statement after insert.

val v = appContext.db.rallyDAO().getVehicleListItem(it.vehicleID)

RoomDatabase

RoomDatabase

@Database(entities = arrayOf(Rally::class, Route::class, CheckPoints::class, Vehicles::class, VehicleListItem::class), version = 1)
abstract class TSDRoom: RoomDatabase() {
    public abstract fun rallyDAO():RallyDAO
}

内部DAO

@Insert(onConflict = OnConflictStrategy.REPLACE)
    fun addVehicleListItem(vehicleListItem:VehicleListItem)

    @Query("select * from vehicles_submitted where vehicle_id LIKE :vehicleID")
    fun getVehicleListItem(vehicleID:String):VehicleListItem

VehicleListItem实体

VehicleListItem Entity

@Entity(tableName = "vehicles_submitted",
        foreignKeys = arrayOf(ForeignKey(entity = Rally::class,
                parentColumns = arrayOf("rally_id"),
                childColumns = arrayOf("rally_id"))))
class VehicleListItem {
    @PrimaryKey
    @ColumnInfo(name = "vehicle_id")
    @SerializedName("vehicle_id")
    var vehicleID : String = ""
    @ColumnInfo(name = "driver_id")
    @SerializedName("driver_id")
    var driverID : String = ""
    @ColumnInfo(name = "vehicle_name")
    @SerializedName("vehicle_name")
    var vehicleName : String = ""
    @ColumnInfo(name = "driver_name")
    @SerializedName("driver_name")
    var driverName : String = ""
    @ColumnInfo(name = "driver_email")
    @SerializedName("driver_email")
    var driverEmail : String = ""

    @ColumnInfo(name = "rally_id")
    @SerializedName("rally_id")
    var rallyID: String = ""

    @ColumnInfo(name = "is_passed")
    @SerializedName("is_passed")
    var isPassed = false

    @ColumnInfo(name = "passing_time")
    @SerializedName("passing_time")
    var passingTime:String=""
}

推荐答案

问题出在线程中. Room不允许您在主线程中运行数据库查询.对insert方法的调用位于try-catch块内部,而我忽略了该异常. 我修复了它,这就是现在的读法.

The problem was in the thread. Room doesn't allow you to run database queries in main thread. The call to insert method was inside of a try-catch block, and I was ignoring the exception. I fixed it, and here's how it reads right now.

doAsync {
                        val addedID = appContext.db.rallyDAO().addVehicleListItem(vehicle)
                        Logger.d("vehicle_lsit_item","Inserted ID $addedID")
        }

我还查看了文档, insert方法可能会返回Long(如果将List传递给insert,则返回Long的List).我还更改了插入的签名,这是修改后的代码

Also, I reviewed the documentation, the insert method may return a Long (or List of Long in case of List passed to insert). I also changed the signature of insert, and here is the modified code

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addVehicleListItem(vehicleListItem:VehicleListItem):Long

P.S.我正在将 anko 用于doAsync

P.S. I am using anko for doAsync

这篇关于如何检查房间数据库中是否插入了数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆