gpt4 book ai didi

sql - Scala,元组的sql插值

转载 作者:行者123 更新时间:2023-11-29 13:12:49 28 4
gpt4 key购买 nike

我有一个看起来像普通 sql 中的查询

Select * from persons 
where (persons.first_name, persons.last_name)
in (('a', 'b'), ('c', 'd'))

我正在尝试从 scala 中执行它

val names = List(("James", "hasi"), ("Michael", "Myers))
sql""" Select * from ${table} where (first_name, last_name) IN ${names}""".stripMargin.map(...)

但是它失败了,因为 scalikejdbc 不知道如何插入元组。

"PSQLException: Can't infer the SQL type to use for an instance scala.Tuple2"

知道如何实现吗?我想我总是可以将元组列表转换为字符串,但问题是 scala 会将其用单引号括起来,将所有内容都作为字符串处理。

最佳答案

我会将此作为答案发布,因为它以不同的方式解决了问题。我最终在 scalikejdbc 中编写了原始 SQL 而没有转义元组“名称”

您可以使用 SQLSyntax 类来做到这一点

val a = SQLSyntax.createUnsafely(names)
sql"${a}" <- not escaped

关于sql - Scala,元组的sql插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741432/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com