普通的旧Ruby数据在File中加载本地

时间:2015-03-24 18:35:13

标签: mysql ruby

问题:MYSQL和RUBY(不带rails?)会出错:

此MySQL版本(Mysql :: Error)

不允许使用used命令

亲爱的朋友们,

  1. 我可以从命令行发出这个,但不是来自我的ruby(没有rails)。

    的MySQL>将数据本地infile'remediation_rewrite.csv'加载到表report.remediation IGNORE 1行(MACHINE_NAME,TYPE_OF_ACTION,SCORE,FILE_PATH,MD5,REGKEY,VThit,action,action_result,action_result_detail,date); 查询正常,455168行受影响,65535警告(22.35秒)

  2. 2)Ruby命令由于以下原因而失败:

    column = "(MACHINE_NAME , TYPE_OF_ACTION , SCORE , FILE_PATH , MD5,  REGKEY , VThit, action , action_result, action_result_detail, date)"
        str = "Load data local infile " + "'" + "remediation_rewrite.csv" + "'" + " into table report.remediation" + " IGNORE 1 lines " + "#{column}" + ";";
        p str
        @con = Mysql.new("172.16.10.193", "myusername","xxx", "reporttdatabase")
        @con.options(Mysql::OPT_LOCAL_INFILE, true)
        rs = @con.query(str)
    

1 个答案:

答案 0 :(得分:2)

根据'mysql2' gem documentation,出于安全原因,您需要将:local_infile指定为true(解释here,并引用here)。

将此替换为您的@con客户端对象:

@con = Mysql2::Client.new(
  host: "172.16.10.193", 
  username: "myusername",
  password: "xxx", 
  database: "reporttdatabase",
  local_infile: true
)