Room delete query does not remove the row in database

12,117

Solution 1

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.

Solution 2

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().

Solution 3

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.

Solution 4

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)

Share:
12,117
Martin
Author by

Martin

Updated on June 15, 2022

Comments

  • Martin
    Martin about 2 years

    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?

  • Bedla
    Bedla about 6 years
    According comments it was different issue and OP solved it, but this answer is pretty OK IMHO. Could you please add some references to documentation for future readers?
  • Nick
    Nick over 5 years
    Code-only answers are discouraged. Please click on edit and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. From Review
  • Leonardo Costa
    Leonardo Costa about 4 years
    This solved to me. Just an observation, the diference for my code was that I didn't put the return (int). I'm using Kotlin with suspend functions.
  • hetsgandhi
    hetsgandhi about 4 years
    @LeonardoCosta happy to help!