Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room @Delete with parameters

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

<i>Error:error: Observable query return type (LiveData, Flowable etc) can only be used with SELECT queries that directly or indirectly (via @Relation, for example) access at least one table.</i>

But I can't use @Delete(WHERE... xxx) So how do I delete a specific row by a parameter?

like image 932
Jack Avatar asked Nov 28 '17 18:11

Jack


People also ask

How do I remove an item from a room database?

In MainActivity , implement the onOptionsItemSelected() method to invoke the deleteAll() method on the WordViewModel object. Run your app. In the Options menu, select Clear all data. All words should disappear.

How do I reset my room database?

But, you can go to yourApp -> Long click -> App Info -> Storage & Cache -> clear both cache and Storage. Clearing storage displays a verification dialog that indicates that databases will be destroyed.

Is Android room an ORM?

Is Android Room an ORM? Room isn't an ORM; instead, it is a whole library that allows us to create and manipulate SQLite databases more easily. By using annotations, we can define our databases, tables, and operations.


5 Answers

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.

like image 168
Maragues Avatar answered Oct 16 '22 16:10

Maragues


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.

like image 37
Awais Avatar answered Oct 16 '22 17:10

Awais


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();
like image 44
Faxriddin Abdullayev Avatar answered Oct 16 '22 16:10

Faxriddin Abdullayev


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);
   }
like image 37
Rohit S Avatar answered Oct 16 '22 17:10

Rohit S


You can now delete using only partial data.

Per the documentation:

@Entity
data class Playlist (
    @PrimaryKey
    val playlistId: Long,
    val ownerId: Long,
    val name: String,
    @ColumnInfo(defaultValue = "normal")
    val category: String
)

data class OwnerIdAndCategory (
    val ownerId: Long,
    val category: String
)

@Dao
public interface PlaylistDao {
    @Delete(entity = Playlist::class)
    fun deleteByOwnerIdAndCategory(varargs idCategory: OwnerIdAndCategory)
}

In this example you can see that they are deleting the Playlist using only the ownerId and the category. You do not even need to use the primary key (playlistId).

The key is to use the @Delete(entity = Playlist::class) annotation.

like image 1
Randy Avatar answered Oct 16 '22 18:10

Randy