Mysql2 ::错误:您的SQL语法中有错误

时间:2012-07-13 08:43:04

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1

我刚开始在铁轨上使用红宝石,我有搜索文本框,然后每次我输入一个apotraphe(')例如测试'这个词......我总是收到错误:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's  word%' OR english_name LIKE '%testing's  word%' OR chinese_name LIKE '%testin' at line 1: SELECT COUNT(DISTINCT `jos_store`.`id`) FROM `jos_store` LEFT OUTER JOIN (SELECT id as store_replenishment, store, MAX(stock_movement) AS stock_movement FROM jos_store_replenishment GROUP BY store) AS replenishment ON replenishment.store = jos_store.id LEFT OUTER JOIN jos_stock_movement ON jos_stock_movement.id = replenishment.stock_movement WHERE (store_id LIKE '%testing's  word%' OR english_name LIKE '%testing's  word%' OR chinese_name LIKE '%testing's  word%')
你可以帮我解决我的问题。

1 个答案:

答案 0 :(得分:1)

尝试转义你的撇号,例如english_name LIKE'%testing'的单词%'

如果你正在使用'%PHRASE%'并且还有另一个'%%PHRASE%,它认为你已经结束了like子句并且给你一个错误 - 如果你使用转义字符逃脱,例如\',它应该工作:所以你的错误短语,如:

<snip /> WHERE (store_id LIKE '%testing\'s  word%' OR english_name LIKE '%testing\'s  word%' OR chinese_name LIKE '%testing\'s  word%')    

注意我使用'%testing'的单词%'来确保它不认为第二个撇号结束了LIKE子句