Room db使用@query更新多行 [英] room db update multiple rows with @query
问题描述
我想更新会议室数据库中的多行.但是我没有对象-我只有ID.
I would like to update multiple rows in a room database. But I don't have the objects - I have only the ids.
如果我更新一行,则会在DAO中写入以下内容:
If I update one row I write something like this to my DAO:
@Query("UPDATE items SET place = :new_place WHERE id = :id;")
fun updateItemPlace(id:Int, new_place:String)
如果有多行,我需要这样的东西:
With multiple rows I would need something like this:
@Query("UPDATE items SET place = :new_place WHERE id = :ids;")
fun updateItemPlaces(ids:List<Int>, new_place:String)
OR
@Query("UPDATE items SET place = :new_place WHERE id IN :ids;")
fun updateItemPlaces(ids:String, new_place:String)
在我的ids-String上写类似(1,4,7,15)"的地方
where I write something like '(1,4,7,15)' to my ids-String
有人可以告诉我进行此类更新的好方法吗
Can someone tell me a good way to make such an update
因为类似
val ids = ListOf(1,4,7,15)
ids.forEach{
itemDao.updateItemPlace(it,'new place')
}
似乎不是一个很好的解决方案
does not seem to be a good solution
推荐答案
@Query("UPDATE items SET place = :new_place WHERE id IN (:ids)")
fun updateItemPlaces(ids:List<Int>, new_place:String)
但是请记住,如果您的ID列表包含超过999个项目,SQLite将引发异常:
But keep in mind if your list of ids contains more than 999 items SQLite will throw an exception:
SQLiteException too many SQL variables (Sqlite code 1)
这篇关于Room db使用@query更新多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!