我設法讓Roo Gem工作,以便我可以導入Excel文件並將其保存到我的應用程序的數據庫中。這已經成功了幾次,但似乎在某些行中包含空格的電子表格中,它會引發錯誤。我的代碼如下:保存空白Excel單元格到Rails Postgres數據庫
需要「袋鼠」
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通過包括方法之前&
它不包括零誤差的方法,所以我希望這會工作。
第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的不是內容與所述空數據被保存在數據庫中,因此「阿賓登&威特尼學院(阿賓登校區)」移動到另一列,因爲相關記錄,如下面的遷移記錄顯示的字符串:
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
難道這是怎麼回事錯了,在那裏有關如何解決這個問題的任何建議?
'錯誤:無效的輸入語法類型的數字:「阿賓登&威特尼學院(阿賓登校區)」 '我可能會讀這個錯誤,但是,這個值不是數字嗎? – whodini9
但架構中的數據既不是數據,也是「文本」,因爲它位於「名稱」列下。 –