ruby on rails:多次插入SQLite3 :: SQLException:near“,”:语法错误:

时间:2013-05-21 15:17:18

标签: sql ruby-on-rails sqlite

我需要在数据库中进行多次插入。它始终显示错误

单次插入效果很好:

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中插入多个记录?(创建记录) 提前致谢

1 个答案:

答案 0 :(得分:0)

看起来SQLite不支持插入多条记录。

https://stackoverflow.com/a/5009740/199712