Doobie示例抱怨列索引不匹配

时间:2020-02-21 13:59:49

标签: scala doobie

尝试stock Doobie example,并因抱怨“无效的列索引”而异常。我的查询非常简单:它从一个Oracle表中选择两列,我希望Doobie将其映射到具有两个匹配属性的案例类实例序列。我已经在SQL IDE中运行了该查询,并且运行良好。我假设PreparedStatement(由Doobie创建)的参数数量与传入的参数数量(一个)不匹配-但我不知道为什么。 这是我第一次与Doobie接触,所以我可能会误解一些简单的内容。

例外是java.sql.SQLException: Invalid column index。知道我在做什么错吗?

import doobie._
import doobie.implicits._
import cats.effect.IO
import scala.concurrent.ExecutionContext

/**
 * @see https://tpolecat.github.io/doobie/
 */
object DoobieExampleOne {
  implicit val cs = IO.contextShift(ExecutionContext.global)
  val jdbcUrl = "jdbc:oracle:thin:@(DESCRIPTION=...)"

  val xa = Transactor.fromDriverManager[IO](
    "oracle.jdbc.driver.OracleDriver", jdbcUrl, "myUser", "myPswd"
  )

  def main(args: Array[String]): Unit = {
    find(idPrefix = "somePfx").transact(xa).unsafeRunSync
  }

  case class SomeEntity(identifier_value: String, some_id: Long)

  def find(idPrefix: String): ConnectionIO[Option[SomeEntity]] =
    // This query runs correctly on the command line
    sql"SELECT identifier_value, some_id FROM some_table WHERE identifier_value LIKE '$idPrefix'"
      .query[SomeEntity].option
}
// Exception in thread "main" java.sql.SQLException: Invalid column index

1 个答案:

答案 0 :(得分:1)

通过更仔细地阅读库存示例来找到解决方案:查询参数不应包含在引号中。正确的语法如下所示:

sql"SELECT identifier_value, some_id FROM some_table WHERE identifier_value LIKE $idPrefix"