我需要在数据库中进行多次插入。它一直显示错误
单次插入完美:
CONN = ActiveRecord::Base.connection
irb(main):271:0> inserts.push'(1,2,3,4,5,6)'
=> ["(1,2,3,4,5,6)"]
sql="INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES #{inserts.join(", ")}"
irb(main):276:0> CONN.execute sql
(32.3ms) INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES (1,2,3,4,5,6)
=> []
多次插入根本不起作用:
irb(main):272:0> inserts.push'(7,8,9,10,11,12)'
=> ["(1,2,3,4,5,6)", "(7,8,9,10,11,12)"]
irb(main):278:0>sql="INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES #{inserts.join(", ")}"
=> "INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES (1,2,3,4,5,6), (7,8,9,10,11,12)"
irb(main):279:0> CONN.execute sql
(0.4ms) INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES (1,2,3,4,5,6), (7,8,9,10,11,12)
ActiveRecord::StatementInvalid: SQLite3::SQLException: near ",": syntax error: INSERT INTO signals_rmas (groupsnorm_id, arraydb_id, probeset_id,signal,updated_at,created_at) VALUES (1,2,3,4,5,6), (7,8,9,10,11,12)
问题是什么?如何在sql中一次插入多条记录?(创建记录)提前致谢