【问题标题】:How to use multiple values for IN clause using same parameter passed through kotlin如何使用通过 kotlin 传递的相同参数为 IN 子句使用多个值
【发布时间】:2020-08-30 12:38:07
【问题描述】:

为什么 IN 子句/运算符无法从表中检索 type1、type2 行?

这是写在 main.kt 文件中

//if i write here **var type="type1"** then the DAO fetches correct result
 but i want to fetch both type1 and type2. So, I am writing it in following way 
and then passing the "type" variable as "bind variable" in DAO 
but nothing is fetched and the output is blank//

var type="'type1','type2'"
var  sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}

这是用 DAO 编写的

@Query("SELECT * FROM tabledata WHERE Subject='English' AND Qtype IN(:types)")
suspend fun engquest(types:String):List<quizdata>

这是数据库。

Question|Subject| Qtype |
------  |-------|------ |
Quest1  |English| type1 |
Quest2  |English| type2 |
Quest3  |English| type3 |

【问题讨论】:

    标签: java sql kotlin android-sqlite android-room


    【解决方案1】:

    您必须传递数组或类型列表:

    var type = listOf("type1", "type2")
    var  sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}
    
    @Query("SELECT * FROM tabledata WHERE Subject='English' AND Qtype IN(:types)")
    suspend fun engquest(types:List<String>):List<quizdata>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-08
      • 2016-12-16
      • 2023-03-09
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      相关资源
      最近更新 更多