我将 PostgreSQL 12.1 与 Scala 和 Doobie 一起使用。尝试使用 LIKE % 语法进行查询时出现异常。它可以在没有 % 的情况下工作。
我的代码:
implicit val cs = IO.contextShift(ExecutionContexts.synchronous)
val driver = "org.postgresql.Driver"
val connectionString = "jdbc:postgresql:postgres"
val user = "postgres"
val pass = "P@ssw0rd"
lazy val xa = Transactor.fromDriverManager[IO](driver, connectionString, user, pass)
def findNamePref(title: String): Option[Book] = {
val s = sql"SELECT * FROM books WHERE title LIKE $title%".query[Book].option
s.transact(xa).unsafeRunSync()
}
例外:
线程“主”org.postgresql.util.PSQLException 中的异常:错误:输入末尾的语法错误位置:org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2505) 的 org.postgresql.core 的 41 .v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2241) 在 org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310) 在 org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:447) 在org.postgresql.jdbc.PgStatement.execute(PgStatement.java:368) 在 org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:158) 在 org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:108)在 doobie.free.KleisliInterpreter$PreparedStatementInterpreter.$anonfun$executeQuery$2(kleisliinterpreter.scala:第956章 猫的.effect.internals.IORunLoop$.startCancelable(IORunLoop.scala:41) at cats.effect.internals.IOBracket$BracketStart.run(IOBracket.scala:86) at cats.effect.internals.Trampoline.cats$effect$internals$ Trampoline$$immediateLoop(Trampoline.scala:70) at cats.effect.internals.Trampoline.startLoop(Trampoline.scala:36) at cats.effect.internals.TrampolineEC$JVMTrampoline.super$startLoop(TrampolineEC.scala:93) at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18) 的cats.effect.internals.TrampolineEC$JVMTrampoline.$anonfun$startLoop$1(TrampolineEC.scala:93)。并发.BlockContext$。与BlockContext(BlockContext.scala:94)在cats.effect.internals.TrampolineEC$JVMTrampoline.startLoop(TrampolineEC.scala:93)在cats.effect.internals.Trampoline.execute(Trampoline.scala:43)在cats.effect.internals .TrampolineEC.execute(TrampolineEC.scala:44) at cats.effect.internals.IOBracket$BracketStart.apply(IOBracket.scala:72) at cats.effect.internals.IOBracket$BracketStart.apply(IOBracket.scala:52) at Cats.effect.internals.IORunLoop$.cats$effect$internals$IORunLoop$$loop(IORunLoop.scala:136) at cats.effect.internals.IORunLoop$RestartCallback.signal(IORunLoop.scala:355) atcats.effect。 internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:376) at cats.effect.internals.IORunLoop$RestartCallback.apply(IORunLoop.scala:316) at cats.effect.internals.IOShift$Tick.run(IOShift.scala:36) 在 java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) 在 java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) 在 java.base /java.lang.Thread.run(Thread.java:834)
依赖项:
scalaVersion := "2.13.1"
lazy val doobieVersion = "0.8.8"
libraryDependencies ++= Seq(
"org.tpolecat" %% "doobie-core" % doobieVersion,
"org.tpolecat" %% "doobie-postgres" % doobieVersion,
"org.tpolecat" %% "doobie-specs2" % doobieVersion
)