Room: could I check passing value to query for NULL?

﹥>﹥吖頭↗ 提交于 2021-02-18 11:38:30

问题


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

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>>

At this moment I had an issue

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

So,

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

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


回答1:


The deal was in this part of the SQL query

WHERE ((:groupIds) IS NULL 

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

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>>


来源:https://stackoverflow.com/questions/52079052/room-could-i-check-passing-value-to-query-for-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!