如何在Kotlin中使用相同参数传递给IN子句的多个值。

huangapple go评论110阅读模式
英文:

How to use multiple values for IN clause using same parameter passed through kotlin

问题

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

这段代码写在main.kt文件中

  1. //如果我在这里写 **var type="type1"**,那么DAO会检索到正确的结果
  2. 但是我想要同时检索type1type2所以我以以下方式编写代码
  3. 然后将变量"type"作为"绑定变量"传递给DAO
  4. 但是没有任何内容被检索出来输出是空白的//
  5. var type="'type1','type2'"
  6. var sq = runBlocking { qdatabase(applicationContext).getquizdao().engquest(type).shuffled() }

这段代码写在DAO中

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

这是数据库。

  1. Question|Subject| Qtype |
  2. ------ |-------|------ |
  3. Quest1 |English| type1 |
  4. Quest2 |English| type2 |
  5. Quest3 |English| type3 |
英文:

Why the IN clause/operator is unable to retrieve type1,type2 rows from the table?

this is written in main.kt file

  1. //if i write here **var type=&quot;type1&quot;** then the DAO fetches correct result
  2. but i want to fetch both type1 and type2. So, I am writing it in following way
  3. and then passing the &quot;type&quot; variable as &quot;bind variable&quot; in DAO
  4. but nothing is fetched and the output is blank//
  5. var type=&quot;&#39;type1&#39;,&#39;type2&#39;&quot;
  6. var sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}

this is written in DAO

  1. @Query(&quot;SELECT * FROM tabledata WHERE Subject=&#39;English&#39; AND Qtype IN(:types)&quot;)
  2. suspend fun engquest(types:String):List&lt;quizdata&gt;

This is the db.

  1. Question|Subject| Qtype |
  2. ------ |-------|------ |
  3. Quest1 |English| type1 |
  4. Quest2 |English| type2 |
  5. Quest3 |English| type3 |

答案1

得分: 2

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

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

You have to pass array or list of types:

  1. var type = listOf(&quot;type1&quot;, &quot;type2&quot;)
  2. var sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}
  1. @Query(&quot;SELECT * FROM tabledata WHERE Subject=&#39;English&#39; AND Qtype IN(:types)&quot;)
  2. suspend fun engquest(types:List&lt;String&gt;):List&lt;quizdata&gt;

huangapple
  • 本文由 发表于 2020年8月30日 20:38:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63657504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定