Room delete query does not remove the row in database

Viewed 12742

I'm using @Query to delete row in my Room database, but I'm not able to delete the record. Here is my query from @Dao

@Dao
public interface NoteDao {

    @Insert
    void insert(final Note note);

    @Update
    void update(final Note note);

    @Query("DELETE FROM notes WHERE uid = :noteId")
    int delete(final int noteId);

    @Query("SELECT * FROM notes")
    LiveData<List<Note>> selectAll();
}

Entity class

@Entity(tableName = "notes")
public class Note {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    @Expose(serialize = false, deserialize = false)
    private int mId;
    @ColumnInfo(name = "uid")
    @SerializedName("id")
    private int mUid;
    @ColumnInfo(name = "text")
    @SerializedName("text")
    private String mText;

    public Note() {
    }

    getters and setters ommited

}

Can someone give me an advice, what I'm doing wrong?

4 Answers

Use delete query like this:

@Query("Delete FROM Orders where quote_no LIKE :quote_no")
void deleteOrderById(String quote_no);

where quote_no is the unique value in the data base with which you want to delete the row and Orders is the table name.

Try using @Query this way.

 @Query("DELETE FROM notes WHERE uid = :arg0")
 int delete(final int noteId);

In the above code of lines, arg0 is the first argument passed to the function delete().

You can use the @Delete annotation and all of the parameters of the Delete method must either be classes annotated with Entity or collections/array of it.

In your case, I believe, that you should use @Delete int delete(Note note) or

@Delete int delete(Note... note)

In my case I've added the wrong delete annotation @DELETE which was imported from retrofit instead of @Delete which is imported from room libraries.

Related