简体   繁体   English

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

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

I have been trying to execute an update query for my PostgreSQL database using Anorm with Play Framework 2.6 in Scala. 我一直在尝试使用Anala和Scala中的Play Framework 2.6为我的PostgreSQL数据库执行更新查询。 The query works fine in pgAdmin so I am not sure what is going wrong here. 查询在pgAdmin中运行正常,所以我不确定这里出了什么问题。 I only want to update a specific column of an entry. 我只想更新条目的特定列。 The wordlistcollection table holds 3 columns: id, title and createddate. wordlistcollection表包含3列:id,title和createddate。

I have tried using both execute() and executeUpdate() as well as adding all required columns but without any success. 我已经尝试使用execute()executeUpdate()以及添加所有必需的列但没有任何成功。

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

EDIT: I've also tried this approach, same result 编辑:我也试过这种方法,结果相同

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()
  }

According to the executeUpdate() function it should return the number of rows affected as Integer, but it returns the following: 根据executeUpdate()函数,它应该返回受Integer影响的行数,但它返回以下内容:

! @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)

I think it has something to do with the returned ResultSet, but I am quite a beginner so no idea how to debug this. 我认为它与返回的ResultSet有关,但我是一个初学者,所以不知道如何调试它。

Thanks for your help! 谢谢你的帮助!

So, turns out I did two things wrong. 所以,事实证明我做了两件事。 As @cchantep pointed out, there should not be single quotes surrounding a string when using string interpolation for building up your SQL queries. 正如@cchantep指出的那样,当使用字符串插值来构建SQL查询时,字符串周围不应该有单引号。 Removing these quotes fixed the problem. 删除这些引号可以解决问题。

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

Weirdly the approach using 奇怪的是使用的方法

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()
  }

Did not work, it gave me another exception. 没有用,它给了我另一个例外。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PSQLException:列索引超出范围:2,列数:1 - PSQLException: The column index is out of range: 2, number of columns: 1 PSQLException:列索引超出范围:7,列数:6 - Postgres初学者 - PSQLException: The column index is out of range: 7, number of columns: 6 - Postgres beginner 如果已存在,则PreparedStatement抛出:org.postgresql.util.PSQLException:列索引超出范围:1,列数:0 - PreparedStatement IF EXISTS throws: org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0 org.postgresql.util.PSQLException:列索引超出范围:2,列数:1 - org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1 org.postgresql.util.PSQLException:列索引超出范围:3,列数:2 - org.postgresql.util.PSQLException: The column index is out of range: 3, number of columns: 2 使用jpql查询jdbc:列索引超出范围:0,列数:1 - querying jdbc using jpql: The column index is out of range: 0, number of columns: 1 错误:列索引超出范围:1,列数:0 - error: The column index is out of range: 1, number of columns: 0 错误:列索引超出范围:1,列数:0 - ERROR : The column index is out of range: 1, number of columns: 0 为什么这会导致“org.postgresql.util.PSQLException:列索引超出范围”异常? - Why does this result in a “org.postgresql.util.PSQLException: The column index is out of range” exception? 列索引超出范围:2,列数:1 更新 jsonb 列时出错 - The column index is out of range: 2, number of columns: 1 error while updating jsonb column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM