1suli0
1suli0

Reputation: 101

Mulitiple row insert in Sequel

Is it possible to insert multiple rows in Sequel, and how it would be done? Can anyone explain it to me?

I've tried something like this:

DB[:table].insert([:x, :y], [[1, 2], [3, 4]])

but it didn't work.

Upvotes: 2

Views: 4052

Answers (2)

Nikhil Patil
Nikhil Patil

Reputation: 487

If you want to use array of value arrays([[1, 2], [3, 4]]) for inserting, instead of array of column,value hashes([{x:1,y:2}, {x:3,y:4}]), you can use Dataset#import.

> DB[:test].import([:x, :y], [[1, 2], [3, 4]])
# => ["INSERT INTO `test` (`x`, `y`) VALUES (1, 2), (3, 4)"]

Upvotes: 0

ymonad
ymonad

Reputation: 12100

You can use Dataset#multi_insert.

Although the document describes that it issues two INSERTs, it seems to issue only one INSERT with multiple values, at least for PostgreSQL.

>> DB[:table].multi_insert([{x:1,y:2}, {x:3,y:4}])
INSERT INTO "table" ("x", "y") VALUES (1, 2), (3, 4)

Upvotes: 7

Related Questions