gpt4 book ai didi

mysql - 更改 Rails CSV 导入到 MYSQL 的日期格式

转载 作者:数据小太阳 更新时间:2023-10-29 08:33:10 25 4
gpt4 key购买 nike

导入 12/1/2011(mm/dd/yyyy) 格式的 MySQL 时,不确定如何更改我的 csv 文件中的日期。 MySQL 在 yyyy/mm/dd 中。

我该怎么做?

我的抽佣文件

require 'csv'
require 'date'

desc "Import gac from csv file"
task :import => [:environment] do
Dir.chdir("#{Rails.root}/lib/assets")
csv_file = "file.csv"
CSV.foreach(csv_file, :headers => true) do |row|
Institution.create({
:company => row[0],
:solveid => row[2],
:phone => row[5],
:other => row[6],
:clientdate => (DateTime.strptime row[7], "%m/%d/%Y").strftime "%Y/%m/%d",
:cunumber => row[1],
:street => row[8],
:city => row[9],
:state_id => row[4],
:zip => row[10],
:source => row[11],
:source2 => row[12],
:demodate1 => (DateTime.strptime row[13], "%m/%d/%Y").strftime "%Y/%m/%d",
:demodate2 => (DateTime.strptime row[14], "%m/%d/%Y").strftime "%Y/%m/%d",
:demodate3 => (DateTime.strptime row[15], "%m/%d/%Y").strftime "%Y/%m/%d",
:client => row[17],
:prospect => row[19],
:alliedlead => row[16],
:notcontacted => row[18]
})
end
end

这给我一个错误。

rake import
rake aborted!
/Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected tSTRING_BEG, expecting '}'
... row[7], "%m/%d/%Y").strftime "%Y/%m/%d",
... ^
/Users/dave/rails_projects/allied_contest/lib/tasks/intitutions.rake:14: syntax error, unexpected ',', expecting keyword_end
...%m/%d/%Y").strftime "%Y/%m/%d",
... ^

最佳答案

   if row[7].present?
date = row[7].split("/")
new_date = "#{date[2]}/#{date[0]}/#{date[1]}"
else
new_date = "N/A"
end

编辑:您收到的错误是因为该行为零。我添加了一张零支票。

关于mysql - 更改 Rails CSV 导入到 MYSQL 的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188832/

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