Android Room @Delete with parameters

后端 未结 3 1455
说谎
说谎 2020-12-15 02:21

I know I can\'t use DELETE in a query (that is a shame by the way), I will get the following error:

Error:error: Observable query retur         


        
相关标签:
3条回答
  • 2020-12-15 02:51

    You can use below method to delete by ID

    @Query("DELETE FROM yourDB WHERE id = :id")
    void deleteById(int id);
    

    for delete all rows

    @Query("DELETE FROM yourDB")
    void delete();
    
    0 讨论(0)
  • 2020-12-15 03:02

    The beauty of room is, we play with the objects. As per requirement you can use for kotlin:

    @Delete
    fun delete(model: LanguageModel)
    

    for Java:

    @Delete
    void delete(LanguageModel model)
    

    it will delete the exact object which is stored in the db with the same values. LanguageModel is my model class and it works perfectly.

    0 讨论(0)
  • 2020-12-15 03:05

    Actually, you can use @Query to perform a delete.

    @Query("DELETE FROM users WHERE user_id = :userId")
    abstract void deleteByUserId(long userId);
    

    Extracted from Query javadoc:

    UPDATE or DELETE queries can return void or int. If it is an int, the value is the number of rows affected by this query.

    0 讨论(0)
提交回复
热议问题