房间:我可以检查传递值以查询 NULL 吗?
Room: could I check passing value to query for NULL?
我正在使用 Room 库,有 DB Stations 并且想执行查询以获取 Stations with/without 过滤器。
更新。我有 DAO,我想在我的数组 (groupIds) 为 null 或空时获取所有记录,如果我在列表中有一个元素,则获取过滤列表。
@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE ((:groupIds) IS NULL OR $GROUP_ID IN(:groupIds))
fun getStationsWithFilter(groupIds: IntArray?): Flowable<List<DbStation>>
此时我遇到了一个问题
08-29 16:09:00.139 9508-9508/->BaseActivity: showError: exception - near ",": syntax error (code 1): , while compiling: SELECT * FROM Stations WHERE ((?,?,?,?,?,?,?,?,?,?) IS NOT NULL
所以,
1) 我可以检查空传递值以进行查询并动态更改 SQL 查询吗?
2) 如果是 - 我遇到的语法问题是什么?
交易在 SQL 查询的这一部分
WHERE ((:groupIds) IS NULL
groupIds - 是一个数组,我在那个地方有一个语法异常。
我的解决方法:我开始将 2 个参数而不是 1 个参数传递给查询,首先 - String 作为 Nullable 标志(我认为它可能是直接布尔值),其次 - 第二个子句的 Int 数组。
@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE :nullableFlag IS NULL OR $GROUP_ID IN (:groupIds))
fun getStationsWithFilter(nullableFlag: String?, groupIds: IntArray?): Flowable<List<DbStation>>
我正在使用 Room 库,有 DB Stations 并且想执行查询以获取 Stations with/without 过滤器。
更新。我有 DAO,我想在我的数组 (groupIds) 为 null 或空时获取所有记录,如果我在列表中有一个元素,则获取过滤列表。
@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE ((:groupIds) IS NULL OR $GROUP_ID IN(:groupIds))
fun getStationsWithFilter(groupIds: IntArray?): Flowable<List<DbStation>>
此时我遇到了一个问题
08-29 16:09:00.139 9508-9508/->BaseActivity: showError: exception - near ",": syntax error (code 1): , while compiling: SELECT * FROM Stations WHERE ((?,?,?,?,?,?,?,?,?,?) IS NOT NULL
所以,
1) 我可以检查空传递值以进行查询并动态更改 SQL 查询吗?
2) 如果是 - 我遇到的语法问题是什么?
交易在 SQL 查询的这一部分
WHERE ((:groupIds) IS NULL
groupIds - 是一个数组,我在那个地方有一个语法异常。
我的解决方法:我开始将 2 个参数而不是 1 个参数传递给查询,首先 - String 作为 Nullable 标志(我认为它可能是直接布尔值),其次 - 第二个子句的 Int 数组。
@Query("SELECT * FROM $STATIONS_TABLE_NAME WHERE :nullableFlag IS NULL OR $GROUP_ID IN (:groupIds))
fun getStationsWithFilter(nullableFlag: String?, groupIds: IntArray?): Flowable<List<DbStation>>