简体   繁体   English

Android会议室数据库,用ID替换元素

[英]Android room database, replace element by id

How to replace existed object by id in the room? 如何用房间中的ID替换现有对象? For example, I have User with id 7 and name John . 例如,我有ID为7的用户,名称为John Then I change name to Bob . 然后我将名称更改为Bob How to update or replace it by id in the room? 如何通过房间中的ID 更新替换它?

Use the update annotation in your Dao, it would update based on the primary key (id in your case) 在您的Dao中使用update注释,它将基于主键(在您的情况下为id)进行更新

@Update()
void updateUser(User user);

You can use REPLACE strategy on conflict and declare insert method this way: 您可以对冲突使用REPLACE策略,并通过以下方式声明插入方法:

@Dao
interface UserDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertUser(user: User)
}

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

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