使用 Anorm returns PSQLException 执行更新:列索引超出范围:2,列数:1

Executing update using Anorm returns PSQLException: The column index is out of range: 2, number of columns: 1

我一直在尝试在 Scala 中使用 Anorm 和 Play Framework 2.6 对我的 PostgreSQL 数据库执行更新查询。该查询在 pgAdmin 中运行良好,所以我不确定这里出了什么问题。我只想更新条目的特定列。 wordlistcollection table 包含 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() 函数,它应该 return 受影响的行数为整数,但它 return 如下:

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

play.api.http.HttpErrorHandlerExceptions$$anon: 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.applyOrElse(AkkaHttpServer.scala:382)
    at play.core.server.AkkaHttpServer$$anonfun.applyOrElse(AkkaHttpServer.scala:380)
    at scala.concurrent.Future.$anonfun$recoverWith(Future.scala:412)
    at scala.concurrent.impl.Promise.$anonfun$transformWith(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(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(SimpleSql.scala:84)
    at anorm.SimpleSql.$anonfun$unsafeStatement$adapted(SimpleSql.scala:84)

我认为这与 returned ResultSet 有关,但我是初学者所以不知道如何调试它。

感谢您的帮助!

原来我做错了两件事。 正如@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()
  }

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