0

这是我使用库Postgres从 scala执行的查询,该库可以正常工作scalikejdbc

import scalikejdbc._
import java.time.LocalDate
def getRange(startDate: LocalDate, endDate: LocalDate): Unit =  {
  implicit val session: AutoSession.type = AutoSession
  log.info(s"startDate = $startDate, endDate = $endDate")
  log.info(sql"""select * from $startDate""".statement)
  val query =
    sql"""select generate_series(
      |timestamp without time zone '2021-05-01',
      |timestamp without time zone '2021-05-03',
      |'1 day'
      |) as tstamp
      |""".stripMargin
  log.info(s"query = ${query.statement}")
  log.info(s"Parameters = ${query.parameters}")
  query.map(rs => rs.string("tstamp")).list().apply().foreach(x => log.info(s"Date = $x"))
}

//-- Output --
Date = 2021-05-01 00:00:00
Date = 2021-05-02 00:00:00
Date = 2021-05-03 00:00:00

但是在对日期使用字符串插值时出现错误。在这种情况下,我使用${startDate.toString}而不是字符串文字2021-05-01

...
def getRange(startDate: LocalDate, endDate: LocalDate): Unit =  {
...
      |timestamp without time zone ${startDate.toString},
      |timestamp without time zone '2021-05-03',
...

我得到的错误是,

SQL execution failed (Reason: ERROR: syntax error at or near "$1" Position: 53): 
select generate_series( timestamp without time zone '2021-05-01', 
timestamp without time zone '2021-05-03', '1 day' ) as tstamp

我在这里缺少什么?...因为错误中显示的 sql 语句确实是有效的

4

1 回答 1

0

这可能看起来像scalikejdbc. 像这样修改查询,确实有效

...
def getRange(startDate: LocalDate, endDate: LocalDate): Unit =  {
...
      |${startDate.toString}::timestamp,
      |${endDate.toString}::timestamp,
...
于 2022-02-07T21:59:00.827 回答