所以我在这里使用光滑的joda-mapper助手:
https://github.com/tototoshi/slick-joda-mapper
当使用joda DateTime比较运行查询时,出现错误。
val today = new DateTime().withTimeAtStartOfDay()
val yesterday = today.minusDays(1)
val yesterdaysRun = ReportRuns.forReport(report).filter {run =>
run.runDate >= yesterday && run.runDate < today
}.sortBy(_.runDate.desc).firstOption
如果我省略过滤器,则此代码可以正常运行,但是使用过滤器时,出现以下错误:
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (unrecognized token: "{")
at org.sqlite.DB.newSQLException(DB.java:383)
at org.sqlite.DB.newSQLException(DB.java:387)
at org.sqlite.DB.throwex(DB.java:374)
at org.sqlite.NativeDB.prepare(Native Method)
at org.sqlite.DB.prepare(DB.java:123)
at org.sqlite.PrepStmt.<init>(PrepStmt.java:42)
at org.sqlite.Conn.prepareStatement(Conn.java:404)
at org.sqlite.Conn.prepareStatement(Conn.java:399)
at com.mchange.v2.c3p0.impl.NewProxyConnection.prepareStatement(NewProxyConnection.java:275)
at scala.slick.jdbc.JdbcBackend$SessionDef$class.prepareStatement(JdbcBackend.scala:123)
at scala.slick.jdbc.JdbcBackend$BaseSession.prepareStatement(JdbcBackend.scala:297)
at scala.slick.jdbc.StatementInvoker.results(StatementInvoker.scala:28)
at scala.slick.jdbc.StatementInvoker.iteratorTo(StatementInvoker.scala:16)
at scala.slick.jdbc.Invoker$class.foreach(Invoker.scala:97)
at scala.slick.jdbc.StatementInvoker.foreach(StatementInvoker.scala:9)
at scala.slick.jdbc.Invoker$class.firstOption(Invoker.scala:44)
at scala.slick.jdbc.StatementInvoker.firstOption(StatementInvoker.scala:9)
at scala.slick.jdbc.UnitInvoker$class.firstOption(Invoker.scala:155)
at scala.slick.driver.JdbcInvokerComponent$UnitQueryInvoker.firstOption(JdbcInvokerComponent.scala:50)
at service.QueryRunnerService$$anonfun$scheduleReports$1$$anonfun$apply$1.apply(QueryRunnerService.scala:57)
...
想知道是否有人可以为我指出正确的解决方案,这是否可能是joda映射器中的SQL转义错误?或者是别的什么?
以下是此生成的SQL查询(加上格式):
SELECT x2."id",
x2."reportid",
x2."query",
x2."run_date",
x2."status",
x2."start_time",
x2."end_time",
x2."error_message",
x2."error_details",
x2."created_at",
x2."updated_at"
FROM "report_runs" x2
WHERE ( x2."reportid" = 1 )
AND ( ( x2."run_date" >= {ts '2014-03-28 00:00:00.0'} )
AND ( x2."run_date" < {ts '2014-03-29 00:00:00.0'} ) )
ORDER BY x2."run_date" DESC
最佳答案
这是Joda Mapper(https://github.com/tototoshi/slick-joda-mapper)中的错误。
实际上,该映射器中有很多代码可以完成非常简单的操作。为了解决这个问题,最好为joda DateTime定义一个简单的自定义列类型,如下所示:
import java.sql.Timestamp
import org.joda.time.DateTime
import org.joda.time.DateTimeZone.UTC
object CustomColumnTypes {
implicit lazy val jodaType = MappedColumnType.base[DateTime, Timestamp](
{d => new Timestamp(d.getMillis)} ,
{d => new DateTime(d.getTime, UTC)}
)
}
确保从您选择的驱动程序中导入相应的
.simple._
。例如:
import scala.slick.driver.SQLiteDriver.simple._
对于最新版本的Slick,Scala和Joda
要导入的软件包:
"org.joda" % "joda-convert" % "2.2.1", // for time convert
"com.github.tototoshi" %% "slick-joda-mapper" % "2.4.2", // 2.4 doesn't work
"joda-time" % "joda-time" % "2.7",
"org.xerial" % "sqlite-jdbc" % "3.30.1", // sqlite driver
光滑包装:
"com.typesafe.slick" %% "slick" % "3.3.2",
"com.typesafe.slick" %% "slick-codegen" % "3.3.2", // for generating Table schema for sqlite db
导入代码:
import slick.jdbc.SQLiteProfile.api._
Database.forURL(url = absoluteDatabaseUrl)
关于sqlite - 为什么在带有Scala Slick和Joda DateTime的SQLite3上出现SQLITE_ERROR?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22703609/