Ruby Mysql2客户端在插入时不反斜杠

时间:2016-08-03 11:58:13

标签: mysql ruby-on-rails ruby ruby-on-rails-4 mysql2

我们正在我们的应用程序中使用生产和登台数据库。 我们的要求是在生产数据库中添加记录时将所有记录插入登台数据库,以便两个服务器保持一致且数据相同。

我使用Mysql2客户端池连接到登台服务器并插入添加到生产中的记录。  这是我的代码:

def create   
    @aperson = Person.new
    @person = @aperson.save
    if @person && Rails.env == "production"
      #add_new_person_to_staging

      client = Mysql2::Client.new(:host => dbconfig[:host], :username => dbconfig[:username], :password => dbconfig[:password], :database => dbconfig[:database])        
      @person_result = client.query('INSERT INTO user_types(user_name,    regex, code) Values ("myname" , "\.myregex\." , "ns" );')
    end
end

此处" @person_result"记录被插入到mysql表中但是" regex"专栏删除" \"斜杠。

喜欢:user_name = myname, regex = .myregex., code = ns

当我手动执行" Insert"在mysql命令行中查询,它与\ slash一起插入。但不是通过" client.query" 为什么\ slash被淘汰了。请帮帮我。

感谢。

2 个答案:

答案 0 :(得分:1)

作为SQL注入保护预处理器的一部分,MySQL可能会删除

\。

您是否尝试过使用双反斜杠或使用escape方法正确转义字符串?

答案 1 :(得分:0)

尝试使用此

@person_result = client.query('INSERT INTO user_types(user_name, regex, code) Values (myname , "\."+myregx+".\" , ns )')
相关问题