这是我使用库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 语句确实是有效的