Room:我可以检查传递的值以查询NULL吗? [英] Room: could I check passing value to query for NULL?

查看:83
本文介绍了Room:我可以检查传递的值以查询NULL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Room libs,具有DB Station,并想执行查询以获取具有/不具有过滤器的Station.

I'm using Room libs, have DB Stations and want to execute query to get Stations with/without filter.

UPD.我有DAO,我想在我的数组(groupIds)为null或为空时获取所有记录,并且如果我在数组中有一个元素,则要获取过滤列表.

UPD. I have DAO and I want to get all the records when my array (groupIds) is null or empty and get filtered list if I have at list one element in array.

@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查询吗?

1) could I check for null passing value to query and dynamically chabge SQL query?

2)如果是,我有什么语法问题?

2) if yes - what the syntax issue I have?

推荐答案

此交易位于SQL查询的这一部分

The deal was in this part of the SQL query

WHERE ((:groupIds) IS NULL 

groupIds -是一个数组,我在那个地方有一个语法异常.

groupIds - is an Array and I had an syntax exception in that place.

我的解决方法:我开始传递2个参数来查询而不是1个,第一个-字符串作为Nullable标志(我认为它可能是直接布尔值),第二个-用于第二个子句的Int数组.

My workaround: I began to pass 2 parameters to query instead of 1, first - String as a Nullable flag (I think it might be a direct Boolean) and second - an array of Int for second clause.

@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:我可以检查传递的值以查询NULL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆