0

我正在尝试按照此导入脚本将国家和城市数据从 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)
4

2 回答 2

0

您正在使用 mysql load 命令尝试导入 sqlite。请参阅:将数据批量加载到 sqlite?有关如何导入 sqlite 的示例。

于 2013-02-09T00:34:56.817 回答
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

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

于 2013-02-09T00:36:24.533 回答