ruby-on-rails - PG::InvalidTextRepresentation:错误:整数的输入语法无效

标签 ruby-on-rails postgresql prepared-statement psql

我在 Rails 上使用准备好的语句执行“IN”查询。我收到 PG::InvalidTextRepresentation 错误。

代码:

def mark_ineligible(match_ids)
  ids = match_ids.join(", ")
  result = epr("mark_matches_as_ineligible",
               "UPDATE matches SET is_eligibile=false WHERE id IN ( $1 )",
               [ids])
end

def epr(statementname, statement, params)
  connection = ActiveRecord::Base.connection.raw_connection
  begin
    result = connection.exec_prepared(statementname, params)
    return result
  rescue PG::InvalidSqlStatementName => e
    begin
      connection.prepare(statementname, statement)
    rescue PG::DuplicatePstatement => e
      # ignore since the prepared statement already exists
    end
    result = connection.exec_prepared(statementname, params)
    return result
  end
end

试图调用它使用:

match_ids = [42, 43]
mark_ineligible match_ids
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "42, 43"

    from (irb):24:in `exec_prepared'
    from (irb):24:in `rescue in epr'
    from (irb):15:in `epr'
    from (irb):8:in `mark_ineligible'
    from (irb):35

请在这里帮忙。我想知道为什么会出现此错误以及如何解决。

谢谢,

最佳答案

mark_ineligible 应如下所示:

def mark_ineligible(match_ids)
    result = epr("mark_matches_as_ineligible",
           "UPDATE matches SET is_eligibile=false WHERE id IN ( $1 )", match_ids)
end

当您调用 mark_ineligible 时,传递一个数组作为参数:

mark_ineligible(match_ids)  #=>match_ids = [42,43]

关于ruby-on-rails - PG::InvalidTextRepresentation:错误:整数的输入语法无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37071228/

相关文章:

python - 检查 psycopg2/postgresql 中是否存在 python 值列表

postgresql - PostgreSQL 是否并行查询分区?

mysql - 在没有 ' ' 的情况下将字符串插入准备好的语句

node.js - PostgreSQL node.js 准备语句最大绑定(bind)

java - 无法使用 PreparedStatement 的 getGeneratedKeys() 在 Java 中找到生成的 key

ruby-on-rails - 在rails中将两个查询相互减去

sql - rails order through count 在其他表上

ruby-on-rails - 如何使用 TagLib 获取歌曲封面

ruby-on-rails - 从多个地方渲染 javascript : The Rails way

reactjs - 仅将 Firebase 用于 Postgres 堆栈中的身份验证