【问题标题】:Ruby Mysql2 Client not taking backslash while insertRuby Mysql2客户端在插入时不带反斜杠
【发布时间】:2016-08-03 11:58:13
【问题描述】:

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

我已使用 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

当我在 mysql 命令行中手动执行“插入”查询时,它会与 \ 斜杠一起插入。但不是通过“client.query” 为什么\ slash 被淘汰了。请在这里帮助我。

谢谢。

【问题讨论】:

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


    【解决方案1】:

    \ 很可能作为 SQL 注入保护预处理器的一部分被 MySQL2 客户端删除。

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

    【讨论】:

    • 是的,我尝试添加 \\ 斜杠,但都被消除了。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 它像这样保存在 mysql 中:%#{myname} %#{.myregex.} %#{ns}
    猜你喜欢
    • 2020-03-13
    • 1970-01-01
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    相关资源
    最近更新 更多