尝试stock Doobie example,得到一个异常,抱怨“无效的列索引”。我的查询非常简单:它从一个Oracle表中选择两列,我希望Doobie将其映射到一系列具有两个匹配属性的case类实例。我在SQL IDE中运行了该查询,它运行得很好。我假设PreparedStatement (由Doobie创建)的参数数量和传入的参数数量(1)之间存在不匹配-但我不知道原因。这是我第一次接触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发布于 2020-02-21 21:59:50
通过更仔细地阅读股票示例找到了解决方案:查询参数不应该用引号括起来。正确的语法如下所示:
sql"SELECT identifier_value, some_id FROM some_table WHERE identifier_value LIKE $idPrefix"https://stackoverflow.com/questions/60340129
复制相似问题