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

时间:2019-05-19 15:32:00

标签: postgresql scala anorm

我一直试图在Scala中使用Anorm和Play Framework 2.6对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有关,但是我是一个初学者,所以不知道如何调试它。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

因此,事实证明我做错了两件事。 正如@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()
  }

没有工作,这给了我另一个例外。