将空白Excel单元格保存到Rails Postgres数据库

时间:2017-01-25 19:45:19

标签: ruby-on-rails ruby postgresql import-from-excel roo-gem

我设法让Roo Gem工作,以便我可以导入Excel文件并将其保存到我的应用程序的数据库中。这已经成功了几次,但似乎在某些行中包含空格的电子表格中会出现错误。我的代码如下:

要求'roo'

xlsx = Roo::Excelx.new(File.expand_path('../Downloads/LOCATION.xlsx'))

xlsx.each_row_streaming(offset: 1) do |row|
    Location.find_or_create_by(ukprn: row[0]&.value, accomurl: row[1]&.value, instbeds: row[2]&.value, instlower: row[3]&.value, instupper: row[4]&.value, locid: row[5]&.value, name: row[6]&.value, lat: row[7]&.value, long: row[8]&.value, locukprn: row[9]&.value, loccountry: row[10]&.value, privatelower: row[11]&.value, privateupper: row[12]&.value, suurl: row[13]&.value)
end

我在Ruby 2.3.0中通过在方法之前包含&来读取它不包含nil方法错误,所以我希望这可行。

前4行看起来工作得很好,但是当它在空白处变成一行时,下面是我得到的错误的片段:

Location Load (1.1ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" IS NULL AND "locations"."instlower" = $3 AND "locations"."instupper" = $4 AND "locations"."locid" = $5 AND "locations"."name" = $6 AND "locations"."lat" = $7 AND "locations"."long" IS NULL AND "locations"."locukprn" = $8 AND "locations"."loccountry" = $9 AND "locations"."privatelower" = $10 AND "locations"."privateupper" = $11 AND "locations"."suurl" = $12 LIMIT $13  [["ukprn", "10000055"], ["accomurl", "http://www.brookes.ac.uk/studying-at-brookes/accommodation/halls-in-detail/"], ["instlower", 4081], ["instupper", "4684"], ["locid", "6463"], ["name", "AB"], ["lat", "Abingdon & Witney College (Abingdon Campus)"], ["locukprn", 51], ["loccountry", "-1.28696"], ["privatelower", 10000055], ["privateupper", "XF"], ["suurl", "4219"], ["LIMIT", 1]]
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR:  invalid input syntax for type numeric: "Abingdon & Witney College (Abingdon Campus)"
: SELECT  "locations".* FROM "locations" WHERE "locations"."ukprn" = $1 AND "locations"."accomurl" = $2 AND "locations"."instbeds" IS NULL AND "locations"."instlower" = $3 AND "locations"."instupper" = $4 AND "locations"."locid" = $5 AND "locations"."name" = $6 AND "locations"."lat" = $7 AND "locations"."long" IS NULL AND "locations"."locukprn" = $8 AND "locations"."loccountry" = $9 AND "locations"."privatelower" = $10 AND "locations"."privateupper" = $11 AND "locations"."suurl" = $12 LIMIT $13
    from /home/dave/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:606:in `exec_prepared'

我认为Postgres不满足于将数据保存在数据库中,因此“Abingdon& Witney College(Abingdon Campus)”被移动到另一列,因为相关记录是一个字符串,如图所示迁移记录如下:

class CreateLocations < ActiveRecord::Migration[5.0]
  def change
    create_table :locations do |t|
      t.text :name
      t.decimal :lat
      t.decimal :long
      t.string :locid, index: true
      t.integer :locukprn
      t.string :loccountry
      t.integer :privatelower
      t.string :privateupper
      t.string :ukprn, index: true
      t.string :suurl
      t.string :accomurl
      t.integer :instbeds
      t.integer :instlower
      t.string :instupper
      t.references :institute, foreign_key: true, index: true
    end
  end
end

这是出了什么问题,有没有关于如何解决这个问题的建议?

1 个答案:

答案 0 :(得分:0)

您的选择包括:

[
 (0)["ukprn", "10000055"],
 (1)["accomurl", "http://www.brookes.ac.uk/studying-at-brookes/accommodation/halls-in-detail/"],
 (2)["instlower", 4081],
 (3)["instupper", "4684"],
 (4)["locid", "6463"],
 (5)["name", "AB"],
 (6)["lat", "Abingdon &amp; Witney College (Abingdon Campus)"],
 (7)["locukprn", 51],
 (8)["loccountry", "-1.28696"],
 (9)["privatelower", 10000055],
 (10)["privateupper", "XF"],
 (11)["suurl", "4219"],
 (12)["LIMIT", 1]
]

此lat值不是数字,您的迁移有t.decimal :lat