6

这是我的完整脚本。

我正在尝试制作一个 rake 任务,该任务从目录中的文件中收集数据并将它们加载到 mysql 中。

我修复了 local-infile = 1,没有任何效果。它只是给了我错误

namespace :db do
  namespace :load do
    desc "Load Properties into DB"
    task :properties => :environment do
      Mysql2::Client.default_query_options[:connect_flags] |= Mysql2::Client::LOCAL_FILES
      @files = Dir.entries("db/property_website_scripts/")
      connection = ActiveRecord::Base.connection()

      for file in @files
        next if file == "." || file == ".."
        sql = "LOAD DATA LOCAL INFILE '#{Rails.root}/db/property_website_scripts/#{file}'
               INTO TABLE properties
               FIELDS TERMINATED BY '|'
               LINES TERMINATED BY '\r\n'
               (property_type,property_for,city,state,country......);"

        connection.execute(sql)
      end

      #updating created at and updated at
      Property.update_all({:created_at => Time.now, :updated_at => Time.now}, "created_at IS NULL")
    end
  end
end
4

2 回答 2

5

这篇文章的解决方案对我有用:启用 local-infile for loading data into remote mysql from rails

将此添加到database.yml

local_infile: true

于 2015-09-23T08:47:54.070 回答
0

我突然遇到了与 gem0.3.11版本相同的错误。mysql2由于我已经使用该版本有一段时间了,我认为这是另一个 gem 的某种依赖问题。尝试升级到 version 0.3.12b5,它为我修复了它。

于 2013-02-19T18:20:58.920 回答