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

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

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

问题

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

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

//如果我在这里写 **var type="type1"**,那么DAO会检索到正确的结果
但是我想要同时检索type1和type2所以我以以下方式编写代码
然后将变量"type"作为"绑定变量"传递给DAO
但是没有任何内容被检索出来输出是空白的//

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 |
英文:

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

this is written in main.kt file

//if i write here **var type=&quot;type1&quot;** 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 &quot;type&quot; variable as &quot;bind variable&quot; in DAO 
but nothing is fetched and the output is blank//

var type=&quot;&#39;type1&#39;,&#39;type2&#39;&quot;
var  sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}

this is written in DAO

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

This is the db.

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

答案1

得分: 2

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

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>
英文:

You have to pass array or list of types:

var type = listOf(&quot;type1&quot;, &quot;type2&quot;)
var  sq = runBlocking {qdatabase(applicationContext).getquizdao().engquest(type).shuffled()}
@Query(&quot;SELECT * FROM tabledata WHERE Subject=&#39;English&#39; AND Qtype IN(:types)&quot;)
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:

确定