简体   繁体   English

Android Room @Delete 带参数

[英]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:我知道我不能在查询中使用DELETE (顺便说一句,这很遗憾),我会收到以下错误:

<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?但是我不能使用@Delete(WHERE... xxx)那么如何通过参数删除特定行呢?

Actually, you can use @Query to perform a delete.实际上,您可以使用@Query来执行删除。

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

Extracted from Query javadoc :查询 javadoc中提取:

UPDATE or DELETE queries can return void or int. UPDATE 或 DELETE 查询可以返回 void 或 int。 If it is an int, the value is the number of rows affected by this query.如果是 int,则值为受此查询影响的行数。

The beauty of room is, we play with the objects.房间的美妙之处在于,我们玩弄物体。 As per requirement you can use for kotlin:根据要求,您可以用于 kotlin:

@Delete
fun delete(model: LanguageModel)

for Java:对于 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. LanguageModel 是我的模型类,它运行良好。

You can use below method to delete by ID您可以使用以下方法按ID删除

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

for delete all rows删除所有行

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

ROOM database provides easy way to INSERT, UPDATE and DELETE an object in the database. ROOM 数据库提供了在数据库中插入、更新和删除对象的简便方法。 To perform thus operation just needed to annotate @Delete.要执行这样的操作,只需要注释@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. DELETE 操作在删除单个对象成功时返回 Int返回 1 否则如果 DELETE 操作不成功则返回 0,添加返回类型是一个好习惯。

KotlinEG.kt KotlineEG.kt

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

javaEG.java javaEG.java

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

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.在此示例中,您可以看到他们仅使用 ownerId 和类别来删除播放列表。 You do not even need to use the primary key (playlistId).您甚至不需要使用主键 (playlistId)。

The key is to use the @Delete(entity = Playlist::class) annotation.关键是使用 @Delete(entity = Playlist::class) 注解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM