Search code examples
androidsqlitekotlinandroid-sqliteandroid-room

How to delete an object from sqlite db with condition of his attribute in Android Room


I want to delete a row (object) from my SQLite db if the attribute lessonId match with one of the object in my db.
I'm using Room.

I don't know which Query I should put.

NotificationDao:

@Dao
interface NotificationDao {
    @Query("SELECT * FROM notifications")
    fun getNotifications(): Single<List<Notification>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun updateNotification(notification: Notification): Completable

    @Query("DELETE")
    fun clearNotification(notification: Notification): Completable
}

Notification class:

@Entity(tableName = "notifications")
data class Notification(
        @PrimaryKey
        var lessonId: String = "",
        var count: Int = 0)

Solution

  • Have you tried to use Delete with condition (but you have to change parameter's type)?

    @Query("DELETE FROM notifications WHERE lessonId = :lessonId")
    fun clearNotification(lessonId: String): Completable