创建用于从 csv 文件导入数据的 rake 任务 [英] creating rake task for importing data from csv file

查看:45
本文介绍了创建用于从 csv 文件导入数据的 rake 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 rake 任务来将 csv 文件数据导入我的数据库 (MySQL).这是我所做的,但它不起作用

I am creating a rake task to import csv file data to my database (MySQL). Here is what I did but It is not working

require 'csv'
namespace :tech do                                                                                                                                          

desc "Import tech from csv file"

task temp: :environment do

file = "tech.csv"

CSV.foreach(file, :headers => true) do |row|
  Temp.create ({
                         :current => row[1],
                         :today => row[2],
                         :week=> row[3],
                         :month => row[4]
                       })
end

结束结束

但是当我运行 rake tech:temp 时它会抛出这个错误

but when I run rake tech:temp it throws this error

Don't know how to build task 'tech:temp'
y/gems/2.0.0/gems/rake-10.3.2/lib/rake/task_manager.rb:62:in `[]'
/p353/lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:149:in `invoke_task'
lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in    top_level'
 /lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'

 /lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'

 /lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'

  /lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
  lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
  /lib/ruby/gems/2.0.0/gems/rake-10.3.2/lib/rake/application.rb:176:in ` standard_exception_handling'
  lib/rake/application.rb:75:in `run'
  /ruby/gems/2.0.0/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
  bin/rake:23:in `load'
  /bin/rake:23:in `<main>'

迁移后,我已经有临时模型和数据库中的相应表

I already have Temp model existing and respective table in database after migration

推荐答案

我在编写 rake 任务以填充数据库中的数据时遇到了同样的问题.在我的情况下,错误是相同的,只是以错误的方式运行 rake 任务而已.

I was having the same issue while writing rake task to populate data in database. In my case the error was same and it was nothing just running the rake task in wrong manner.

我猜你也在做同样的事情,根据我能猜到的错误

I guess you are doing the same, as per the error I can guess

您正在运行 rake tech:temp,其中任务是临时的,名称空间是技术,这是错误的,您应该将其传递给其他人,因为首先您需要提供任务名称然后是名称空间.

You are running rake tech:temp in which task is temp and namespace is tech, which is wrong you should pass it other was as first you need to give task name then namespace.

所以正确的命令是

rake temp:tech

希望这会奏效.我知道这很傻

It hope this will work. It is silly I know

这篇关于创建用于从 csv 文件导入数据的 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆