Skip to content

Inserting Multiple Rows At Once #638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mohamede1945 opened this issue Apr 26, 2017 · 10 comments
Closed

Inserting Multiple Rows At Once #638

mohamede1945 opened this issue Apr 26, 2017 · 10 comments
Milestone

Comments

@mohamede1945
Copy link

I tried to search the docs but found nothing, is it possible to insert multiple rows at once?

@yasinkavakli
Copy link

Could you be more specific or what's to goal that you want to achieve?

@mohamede1945
Copy link
Author

How can I do something like that:

INSERT INTO mytable (col1, col2, col3) VALUES
    (1, 2, "abc"),
    (2, 4, "xyz"),
    (3, 5, "aaa"),
    (4, 7, "bbb");

@yasinkavakli
Copy link

You could do it with an SQL statement like you've wrote.

I've posted a sample SQL-Statement here: #626

Or you could write a loop and do it with the SQLite way from the documentation:

for user in usersArray {
    try db.run(users.insert(email <- user.email, name <- user.name))
    // INSERT INTO "users" ("email", "name") VALUES (UserEmail, UserName)
}

I haven't tested this.

@mohamede1945
Copy link
Author

That's just workarounds I already did use plain sql statements, but would be nice to have it supported.

@yasinkavakli
Copy link

Well maybe @stephencelis or one of the contributors will comment on this, but I don't see the loop-way as a workaround. I would go that way.

@jberkel jberkel added this to the 0.12.0 milestone Oct 23, 2017
@popwarfour
Copy link

would like this feature also

@mikeDaoud
Copy link

mikeDaoud commented Aug 14, 2018

That would be an awesome feature ... I've searched the docs for something similar but had to use an SQL statement at the end

@virtualrizwan
Copy link

Is there still a way to insert multiple rows in one go ?

@tudor-zugravu
Copy link

tudor-zugravu commented Jun 24, 2019

You can add them in a single transaction:

try db!.transaction {
    for user in usersArray {
        try db.run(users.insert(email <- user.email, name <- user.name))
        // INSERT INTO "users" ("email", "name") VALUES (UserEmail, UserName)
    }
}

@jberkel jberkel modified the milestones: 0.12.0, 0.13.1 Aug 23, 2021
@jberkel
Copy link
Collaborator

jberkel commented Aug 24, 2021

this was merged in #1048

@jberkel jberkel closed this as completed Aug 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants