![](/img/trans.png)
[英]PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “aaa”
[英]PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer
我正在使用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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.