gpt4 book ai didi

ruby-on-rails-3 - 导入 csv 文件后,在我的 sqlite db 表中创建了额外的行

转载 作者:行者123 更新时间:2023-12-03 17:59:18 25 4
gpt4 key购买 nike

我正在通过 rake 任务将 17 个 csv 文件导入我的应用程序。其他文件将正常导入并且不会创建额外的行,但由于某种原因,该文件在我的数据库中创建了大约 60 个空行。我尝试导入的信息运行良好,并且以应有的方式与其适当的表相关联。因此,如果您要使用该应用程序,一切都会正常运行,但会弄乱我的 table 。

例如,我将 2 行导入到表中,并且在创建这两行之后,紧随其后的是 58 个空白行。有没有人对我如何使用条件语句来防止这种情况发生有任何建议,请具体说明我在这方面还是比较新的。谢谢

这是我的代码:

require 'csv'

namespace :import_mat_lists_csv do

task :create_mat_lists => :environment do
puts "Import Material List"

csv_text = File.read('c:/rails/thumb/costrecovery/lib/csv_import/mat_lists.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each_with_index do |row,index|
row = row.to_hash.with_indifferent_access
MatList.create!(row.to_hash.symbolize_keys)
matlist = MatList.last
if @report_incident_hash.key?(matlist.report_nr)
matlist.incident_id = "#{@report_incident_hash[matlist.report_nr]}"
end

#matlist.timesheet_id = @timesheet_id_array[index]
matlist.save
end
end
end

最佳答案

有几种方法可以解决这个问题,但也许最简单的方法是添加:

unless row.join.blank?

您的代码将显示为:
csv.each_with_index do |row,index|
unless row.join.blank?
row = row.to_hash.with_indifferent_access
MatList.create!(row.to_hash.symbolize_keys)
matlist = MatList.last

if @report_incident_hash.key?(matlist.report_nr)
matlist.incident_id = "#{@report_incident_hash[matlist.report_nr]}"
end

#matlist.timesheet_id = @timesheet_id_array[index]
matlist.save
end
end

关于ruby-on-rails-3 - 导入 csv 文件后,在我的 sqlite db 表中创建了额外的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13139567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com