我在Rails上创建了一个构建SQL查询的方法,但是我得到了一个语法错误,不知道为什么,代码看起来是正确的。也许有人能找到问题所在?很可能是我没注意到的很明显的事情。
我的代码:
def build_query(info_arr)
<<~SQL
SELECT * FROM word_group_values WHERE account_id = :account_id
AND word_group_id <> :word_group_id
AND ("title", location) IN #{info_arr.join(', ')}
SQL
end
我得到的错误:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near ")" LINE 2: AND ("title", location) IN () ^ : SELECT * FROM word_group_values WHERE account_id = 110
AND ("title", location) IN ()
2条答案
按热度按时间fhity93d1#
我不知道为什么你需要SQL字符串,但通过两列过滤记录,你可以通过这个原则构建SQL查询:
这意味着你需要在字符串中使用内外括号
不要忘记清理SQL查询
这样称呼它
ff29svar2#
您的查询
因为
info_arr
是空的,所以你得到的是一个空字符串,这是一个无效的查询。(
syntax error at or near ")
正在抱怨空的()
)你需要传递一个元组数组来使它有效。在这种情况下,普通字符串可能不起作用,除非
info_arr
是这样,但它是空的。