将数据从一个表复制到另一个表

时间:2013-03-29 11:47:51

标签: ruby-on-rails

我有两个表,与我的应用程序关联的rails应用程序invtypesitems表没有关联。

invtypes有20.000条记录需要移至items表。

现在我的第一次尝试是简单地在invtypes上执行ActiveRecord查询以获取所有条目,然后通过实例化新的Item添加属性然后调用{来遍历每个条目并创建新的Item model {1}}

然而,这也会进行验证,而其他一些事情会减慢,因为我会在model.save中执行30分钟的任务,因为我会这样做几次,所以不能让它变得那么慢。

所以我的第二个想法是尝试使用普通的旧查询没有模型然而现在这给出了一个错误:

Rails Console

由于Time.now

  ←[1m←[35m (27.0ms)←[0m  INSERT INTO items (typeID, name, description, volume, price, created_at, updated_at) VALUES (0, '#System', '', 0.0, 0.0, '20
13-03-29 04:06:27 +0100', '2013-03-29 04:06:27 +0100');
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2013-03-29 04:06:27 +0100' for column 'created_at' at row 1: INSERT INTO ite
ms (typeID, name, description, volume, price, created_at, updated_at) VALUES (0, '#System', '', 0.0, 0.0, '2013-03-29 04:06:27 +0100', '2013-03-29 04:
06:27 +0100');
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `query'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `block in execute'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in
`block in log'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrume
nt'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in
`log'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `execute'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:214:in `e
xecute'
        from (irb):21:in `block in irb_binding'
        from (irb):19:in `each'
        from (irb):19
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
        from script/rails:6:in `require'

我如何使用Ruby / Rails来解决这个问题,或者我应该用Java编写应用程序C执行此查询?

只执行10行然后抛出:

result = ActiveRecord::Base.connection.select_all( "SELECT * FROM invtypes" )
time = Time.now
result.each do |row|
 sql = "INSERT INTO items (typeID, name, description, volume, price, created_at, updated_at) VALUES (#{row["typeID"]}, '#{row["typeName"]}', '#{row["description"]}', #{row["volume"]}, #{row["basePrice"]}, '#{time}', '#{time}');"
 ActiveRecord::Base.connection.execute(sql)
end

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

time = Time.now.to_formatted_s(:db)

答案 1 :(得分:0)

循环中的SQL查询和插入很慢。尝试使用INSERT..SELECT,该命令将插入您使用第二个语句选择的所有行。我不知道你在使用什么数据库,但是例如,如果你使用的是MySQL,你可以看一下the documentation of the statement