Android Room @Delete with parameters

We Are Going To Discuss About Android Room @Delete with parameters. So lets Start this Java Article.

Android Room @Delete with parameters

  1. Android Room @Delete with parameters

    The beauty of room is, we play with the objects. As per requirement you can use
    for kotlin:
    @Delete fun delete(model: LanguageModel)

  2. Android Room @Delete with parameters

    The beauty of room is, we play with the objects. As per requirement you can use
    for kotlin:
    @Delete fun delete(model: LanguageModel)

Solution 1

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.

Original Author Maragues Of This Content

Solution 2

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.

Original Author Awais Of This Content

Solution 3

You can use below method to delete by ID

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

for delete all rows

@Query("DELETE FROM yourDatabaseTable")
void delete();

Original Author Fakhriddin Abdullaev Of This Content

Solution 4

ROOM database provides easy way to INSERT, UPDATE and DELETE an object in the database. To perform thus operation just needed to annotate @Delete. The DELETE operation returns the Int when deletion of the single object is successful returns 1 else returns 0 if the DELETE operation is unsuccessful, Adding the return type is a good practice.

KotlinEG.kt

   @Dao
   interface EntityLocalDAO {
       @Delete
       fun deleteData(entityObject: EntityObject) : Int
   }

javaEG.java

   @Dao
   interface EntityLocalDAO {
       @Delete
       int deleteData(EntityObject entityObject);
   }

Original Author Rohit S Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment