【问题标题】:Executing update using Anorm returns PSQLException: The column index is out of range: 2, number of columns: 1使用 Anorm 执行更新返回 PSQLException:列索引超出范围:2,列数:1
【发布时间】:2019-10-06 03:17:43
【问题描述】:

我一直在尝试使用 Anorm 和 Play Framework 2.6 在 Scala 中为我的 PostgreSQL 数据库执行更新查询。该查询在 pgAdmin 中运行良好,所以我不确定这里出了什么问题。我只想更新条目的特定列。 wordlistcollection 表包含 3 列:id、title 和 createddate。

我尝试同时使用execute()executeUpdate() 以及添加所有必需的列,但没有任何成功。

override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = '${wordListCollection.title}'
      WHERE id = ${wordListCollection.id};
    """.executeUpdate()
  }

编辑:我也尝试过这种方法,结果相同

override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = {title}
      WHERE id = {id}
    """
    .on(
        "id" -> wordListCollection.id,
        "title" -> wordListCollection.title)
    .executeUpdate()
  }

根据executeUpdate() 函数,它应该以整数形式返回受影响的行数,但它返回以下内容:

! @7c21ckgga - Internal server error, for (PUT) [/api/lists] ->

play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[PSQLException: The column index is out of range: 3, number of columns: 2.]]
    at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:251)
    at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:178)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:382)
    at play.core.server.AkkaHttpServer$$anonfun$1.applyOrElse(AkkaHttpServer.scala:380)
    at scala.concurrent.Future.$anonfun$recoverWith$1(Future.scala:412)
    at scala.concurrent.impl.Promise.$anonfun$transformWith$1(Promise.scala:37)
    at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
    at akka.dispatch.BatchingExecutor$AbstractBatch.processBatch(BatchingExecutor.scala:55)
    at akka.dispatch.BatchingExecutor$BlockableBatch.$anonfun$run$1(BatchingExecutor.scala:91)
    at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.java:12)
Caused by: org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2.
    at org.postgresql.core.v3.SimpleParameterList.bind(SimpleParameterList.java:65)
    at org.postgresql.core.v3.SimpleParameterList.setBinaryParameter(SimpleParameterList.java:132)
    at org.postgresql.jdbc.PgPreparedStatement.bindBytes(PgPreparedStatement.java:983)
    at org.postgresql.jdbc.PgPreparedStatement.setLong(PgPreparedStatement.java:279)
    at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.setLong(HikariProxyPreparedStatement.java)
    at anorm.ToStatementPriority0$longToStatement$.set(ToStatementMisc.scala:197)
    at anorm.ToStatementPriority0$longToStatement$.set(ToStatementMisc.scala:196)
    at anorm.DefaultParameterValue.set(ParameterValue.scala:40)
    at anorm.SimpleSql.$anonfun$unsafeStatement$3(SimpleSql.scala:84)
    at anorm.SimpleSql.$anonfun$unsafeStatement$3$adapted(SimpleSql.scala:84)

我认为它与返回的 ResultSet 有关,但我是一个初学者,所以不知道如何调试。

感谢您的帮助!

【问题讨论】:

  • 存在对 Anorm 插值的误用,因为它将值作为 JDBC 参数安全地插值,而不是像普通插值那样作为字符串,因此对于任何 PreparedStatement 引号都不能手动指定(... 围绕 @ 987654328@)。此外,; 绝对不能存在(至于任何 JDBC PreparedStatement
  • 好吧,我做到了,但它不能解决问题@cchantep
  • 表示根异常org.postgresql.util.PSQLException的完整跟踪,这里只给出消息
  • 添加了完整的堆栈跟踪

标签: postgresql scala anorm


【解决方案1】:

所以,事实证明我做错了两件事。 正如@cchantep 指出的那样,在使用字符串插值来构建 SQL 查询时,字符串周围不应有单引号。删除这些引号解决了这个问题。

 override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = ${wordListCollection.title}
      WHERE id = ${wordListCollection.id}
    """
    .executeUpdate()
  }

奇怪的是使用方法

override def update(wordListCollection: WordListCollection): Int = db.withConnection { implicit c =>
    SQL"""
      UPDATE wordlistcollection
      SET title = {title}
      WHERE id = {id}
    """
    .on(
        "id" -> wordListCollection.id,
        "title" -> wordListCollection.title)
    .executeUpdate()
  }

没有用,它给了我另一个例外。

【讨论】:

  • 保持简单,要么使用 SQL(..) 函数和 {placeholder}s,要么使用 Anorm 插值 SQL"..",混合添加无用的复杂性(以及根据目标 JDBC 驱动程序的不可预测的结果)。
猜你喜欢
  • 2023-03-25
  • 2020-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 2015-02-13
相关资源
最近更新 更多