Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if data is inserted in Room Database

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

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

Inside 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

@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=""
}
like image 998
Rivu Chakraborty Avatar asked Jul 28 '17 07:07

Rivu Chakraborty


People also ask

How do you check data is exist or not in room database?

For check data exists or not you can get int value. @Query("SELECT * FROM TableName WHERE userName = :userName") int isDataExist(String userName); call this query using below code.

How do I find my room database?

Go To App Inspection . Then select the process. Database Inspector will now show the database contents. in 2021, with current Android Studio, this should be the only accepted answer.


1 Answers

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")
        }

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. I am using anko for doAsync

like image 161
Rivu Chakraborty Avatar answered Sep 30 '22 17:09

Rivu Chakraborty