【发布时间】: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@)。此外,;绝对不能存在(至于任何 JDBCPreparedStatement) -
好吧,我做到了,但它不能解决问题@cchantep
-
表示根异常
org.postgresql.util.PSQLException的完整跟踪,这里只给出消息 -
添加了完整的堆栈跟踪
标签: postgresql scala anorm