Rails导入城市,国家,地区数据

时间:2013-02-09 00:19:09

标签: ruby-on-rails ruby-on-rails-3

我正在尝试按照此导入脚本将国家和城市数据从GeoWorldMaps导入我的Rails应用程序。这给了我一些问题。

我用Rails.root替换了RAILS_ROOT。我仍然收到以下错误(见下文)

我正在按照此处详述的步骤进行操作:

http://blog.inspired.no/populate-your-database-with-free-world-cities-countries-regions-in-2-minutes-using-a-rails-migration-273/

以下是迁移文件:

    class Places < ActiveRecord::Migration
  def self.up
    create_table :countries do |t|
      t.string :name, :limit => 50, :null => false
      t.string :fips104, :limit => 2, :null => false
      t.string :iso2, :limit => 2, :null => false
      t.string :iso3, :limit => 3, :null => false
      t.string :ison, :limit => 4, :null => false
      t.string :internet, :limit => 2, :null => false
      t.string :capital, :limit => 25
      t.string :map_reference, :limit => 50
      t.string :nationality_singular, :limit => 35
      t.string :nationaiity_plural, :limit => 35
      t.string :currency, :limit => 30
      t.string :currency_code, :limit => 3
      t.integer :population
      t.string :title, :limit => 50
      t.string :comment, :limit => 255

      t.timestamps
    end

    create_table :regions do |t|
       t.references :country, :null => false
       t.string :name, :limit => 45, :null => false
       t.string :code, :limit => 8, :null => false
       t.string :adm1code, :limit => 4, :null => false

       t.timestamps
     end

     create_table :cities do |t|
       t.references :country, :null => false
       t.references :region, :null => false
       t.string :name, :limit => 45, :null => false
       t.float :latitude, :null => false
       t.float :longitude, :null => false
       t.string :timezone, :limit => 10, :null => false
       t.integer :dma_id
       t.string :county, :limit => 25
       t.string :code, :limit => 4

       t.timestamps
     end
     add_index :cities, :name


     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Countries.txt' INTO TABLE countries
       FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"

     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Regions.txt' INTO TABLE regions
       FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"

     execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Cities.txt' INTO TABLE cities
       FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"


  end

  def self.down
    drop_table :countries
    drop_table :regions
    drop_table :cities
  end
end

我得到的错误是:

SQLite3::SQLException: near "LOAD": syntax error: LOAD DATA INFILE '/Users/william/Websites/project7/db/migrate/Countries.txt' INTO TABLE countries
       FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '
' IGNORE 1 LINES;
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `initialize'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `new'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:91:in `prepare'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/sqlite3-1.3.7/lib/sqlite3/database.rb:134:in `execute'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `block in execute'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activesupport-3.2.11/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/sqlite_adapter.rb:278:in `execute'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:438:in `say_with_time'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:458:in `method_missing'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:334:in `method_missing'
/Users/william/Websites/project7/db/migrate/20130208235628_places.rb:49:in `up'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:370:in `up'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block (2 levels) in migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:410:in `block in migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:129:in `with_connection'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:389:in `migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:528:in `migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `call'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `block in ddl_transaction'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:775:in `ddl_transaction'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:719:in `block in migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `each'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:700:in `migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:570:in `up'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/migration.rb:551:in `migrate'
/Users/william/.rvm/gems/ruby-1.9.3-p125@clasee/gems/activerecord-3.2.11/lib/active_record/railties/databases.rake:179:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

2 个答案:

答案 0 :(得分:0)

您正在使用mysql加载命令尝试导入sqlite。有关如何导入sqlite的示例,请参阅:Bulk load data into sqlite?

答案 1 :(得分:0)

我最好的猜测是sqlite不喜欢LOAD命令,我不确定你是否可以在sqlite shell之外执行以下操作。

要使用sqlite导入csv,请执行以下操作:

.mode csv <table> 
.import <filename> <table>

或者在您的情况下,特别是:

.mode csv Countries
.import /Users/william/Websites/project7/db/migrate/Countries.txt Countries

你需要为城市和州做同样的事情。