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

Saving Blank Excel Cells to Rails Postgres Database

我已经设法让 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" =  AND "locations"."accomurl" =  AND "locations"."instbeds" IS NULL AND "locations"."instlower" =  AND "locations"."instupper" =  AND "locations"."locid" =  AND "locations"."name" =  AND "locations"."lat" =  AND "locations"."long" IS NULL AND "locations"."locukprn" =  AND "locations"."loccountry" =  AND "locations"."privatelower" =  AND "locations"."privateupper" =  AND "locations"."suurl" =  LIMIT   [["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" =  AND "locations"."accomurl" =  AND "locations"."instbeds" IS NULL AND "locations"."instlower" =  AND "locations"."instupper" =  AND "locations"."locid" =  AND "locations"."name" =  AND "locations"."lat" =  AND "locations"."long" IS NULL AND "locations"."locukprn" =  AND "locations"."loccountry" =  AND "locations"."privatelower" =  AND "locations"."privateupper" =  AND "locations"."suurl" =  LIMIT 
    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

这是哪里出了问题,有什么解决方法的建议吗?

您的 Select 有:

[
 (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]
]

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