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

时间:2022-09-24 19:00:35

I have two tables one is not associated with my rails application invtypes and items table which is associated with my application.

我有两个表,一个与我的rails应用程序invtypes和与我的应用程序关联的items表没有关联。

Table invtypes has 20.000 records that i need to move to items table.

表invtypes有20.000条记录,我需要移动到items表。

Now my first attempt was to simply do a ActiveRecord query on invtypes to get all entries and then go over each entry and create a new Item by instantiating a new Item model adding attributes and then calling model.save

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

However this also does validation and few other things that slow down making this a 30min task executed in Rails Console since i will be doing this a few times i can't have it be so slow.

然而,这也做了验证,并且很少有其他事情减慢使得在Rails控制台中执行这个30分钟的任务,因为我将这样做几次我不能让它这么慢。

So my second idea was to try this with plain old query no Models however this now gives an error:

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

  ←[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'

Due to Time.now

由于Time.now

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

How can i go about this with Ruby/Rails or should I write a app in Java | C to execute this query ?

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

Only executes 10 rows then throws:

只执行10行然后抛出:

        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):7:in `block in irb_binding'
        from (irb):4:in `each'
        from (irb):4
        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'
        from script/rails:6:in `<main>'

2 个解决方案

#1


0  

You could try this:

你可以试试这个:

time = Time.now.to_formatted_s(:db)

#2


0  

SQL queries and inserts within a loop are slow. Try with INSERT..SELECT, that command will insert all the rows that you selected with the second statement. I don't know what DB are you using, but for example, if you are using MySQL you can take a look at the documentation of the statement.

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

#1


0  

You could try this:

你可以试试这个:

time = Time.now.to_formatted_s(:db)

#2


0  

SQL queries and inserts within a loop are slow. Try with INSERT..SELECT, that command will insert all the rows that you selected with the second statement. I don't know what DB are you using, but for example, if you are using MySQL you can take a look at the documentation of the statement.

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