ActiveRecord::Base.connection.execute
( "SELECT * FROM search_results(p_limit := 12,
p_offset := 0,p_blocked_ids := '',p_following_ids
:= '54,171,174',p_sort := 'latest',
p_tags := '', p_search := '(O'neil)')" )
p_search
参数中的特殊字符失败。我尝试使用双引号和$$ $$
进行引号,但这不能解决问题。
在此查询中转义特殊字符的正确方法是什么?
完整错误消息:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "neil"
LINE 1: ...t := 'latest', p_tags := '', p_search := '(O'neil)')
^
: SELECT * FROM search_results(p_limit := 12, p_offset := 0,p_blocked_ids := '',p_following_ids := '54,171,174',p_sort := 'latest', p_tags := '', p_search := '(O'neil)')
from /Users//.gem/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
from /Users//.gem/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
from /Users//.gem/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:590:in `block in log'
from /Users//.gem/ruby/2.3.0/gems/activesupport-5.0.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users//.gem/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:583:in `log'
from /Users//.gem/ruby/2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execu
在这种情况下,假定重复项的建议没有帮助,并返回', p_search := ''o''neil'')
答案 0 :(得分:0)
尝试一下:
ActiveRecord::Base.connection.execute
( "SELECT * FROM search_results(p_limit := 12,
p_offset := 0,p_blocked_ids := '',p_following_ids
:= '54,171,174',p_sort := 'latest',
p_tags := '', p_search := '(O\'neil)')" )
请注意\
中多余的(O\'neil)'
。
我认为PostgreSQL正在读取'(O'neil)'
,并且由于(O
后面有一个单引号,所以该字符串在此结束并且会抱怨语法。