无法迁移.csv文件

时间:2011-03-24 20:35:38

标签: mysql ruby-on-rails csv

我的迁移文件:

    def self.up
create_table :geo_data do |t|
  t.column :zip_code,  :string
  t.column :latitude,  :float
  t.column :longitude, :float
  t.column :city,      :string
  t.column :state,     :string
  t.column :county,    :string
  t.column :type,      :string
end
add_index "geo_data", ["zip_code"], :name => "zip_code_optimization"

csv_file = "#{RAILS_ROOT}/db/migrate/geo_data.csv"
fields = '(zip_code, latitude, longitude, city, state, county)'

execute "LOAD DATA INFILE '#{csv_file}' INTO TABLE geo_data FIELDS " +
        "TERMINATED BY ',' OPTIONALLY ENCLOSED BY \"\"\"\" " +
        "LINES TERMINATED BY '\n' " + fields
end

当我尝试使用文件填充数据库时出现此错误:

   Mysql2::Error: Can't get stat of '/home/sean/project/db/migrate/geo_data.csv' (Errcode: 13): LOAD DATA INFILE '/home/sean/project/db/migrate/geo_data.csv' INTO TABLE geo_data FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY """" LINES TERMINATED BY '
   ' (zip_code, latitude, longitude, city, state, county)

/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters   /abstract_adapter.rb:207:in `log'
/usr/lib/ruby/gems/1.8/gems/mysql2-0.2.6/lib/active_record/connection_adapters/mysql2_adapter.rb:314:in `execute'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:383:in `send'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:383:in `method_missing'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:359:in `say_with_time'
 /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:359:in `say_with_time'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:379:in `method_missing'
 /home/sean/project/db/migrate/20110324194138_geo_data.rb:19:in `up_without_benchmarks'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:312:in `send'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:312:in `migrate'
 /usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:312:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:395:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:395:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:537:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:613:in `call'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:613:in `ddl_transaction'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:536:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:523:in `each'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:523:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:433:in `up'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/migration.rb:415:in `migrate'
 /usr/lib/ruby/gems/1.8/gems/activerecord-3.0.5/lib/active_record/railties/databases.rake:142
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
 /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
 /usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
 /usr/bin/rake:19:in `load'
 /usr/bin/rake:19

1 个答案:

答案 0 :(得分:0)

LOAD DATA INFILE...更改为LOAD DATA LOCAL INFILE..