我正在使用我的項目中新架構組件的房間數據庫。 我正在通過dao添加一些數據,但是當試圖檢索它時,我沒有收到它。你能否建議我如何檢查插入是否成功?以下是幫助您理解問題的代碼。如何檢查數據是否插入到房間數據庫中
添加到數據庫,我用調試器檢查,這個語句執行成功。
appContext.db.rallyDAO().addVehicleListItem(vehicle)
插入後在此語句中從數據庫獲取null。
val v = appContext.db.rallyDAO().getVehicleListItem(it.vehicleID)
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實體
@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=""
}