【问题标题】:how to Update records in database using where condition in android room?如何使用 android room 中的 where 条件更新数据库中的记录?
【发布时间】:2020-12-14 13:41:42
【问题描述】:

我正在使用 android room 库来维护本地数据库。 我正在尝试通过检查列值来更新表的一行,就像 where 子句一样。

目前我正在使用下面的代码来更新记录,

@Update
void update(ProductInfo prodInfo);

我的问题是,我想使用where子句来更新记录,例如我的查询是;

Update ProductInfo set (AllColumns) where prodInfo.last_name = 'xyz';

但在房间里我找不到答案,一种选择是编写原始查询,但编写所有列名非常冗长。

谁能指导我解决这个问题?

【问题讨论】:

  • 1.使用原始查询或 2. 使用“where”全选,更新 java 中的字段,使用更新列表 @Update void updateList(List<ProductInfo> prodInfo);
  • @Selvin :我尝试实现这一点,但由于数据不同,它正在创建另一个新记录。
  • 不要更改主键,那么第二个解决方案应该可以正常工作

标签: android database android-room


【解决方案1】:

您可以使用@Query("your update query") 来解决您的问题。

【讨论】:

  • 你能否举个例子或 sn-p 我如何为 @Query 部分中的所有列编写 ProductInfo 更新查询..
  • `@Query("Update ProductInfo set (AllColumns) where prodInfo.last_name =:lastname") void update(String lastname, ..columns)
  • mehmet 感谢您回复此帖。有 43 列,这就是为什么我正在寻找一些简单的方法。这里 ProductInfo 如果我的模型类定义为 @Entity。
  • 我不知道有什么其他方法可以做到这一点
猜你喜欢
  • 2023-02-24
  • 1970-01-01
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多