Skip to content
This repository has been archived by the owner on May 10, 2020. It is now read-only.

Example of batch insert #93

Open
cfsamson opened this issue Feb 11, 2019 · 2 comments
Open

Example of batch insert #93

cfsamson opened this issue Feb 11, 2019 · 2 comments

Comments

@cfsamson
Copy link

I'm trying to bacth insert a large number of data to the sql server, but I cant figure out a good way to do this with this driver. Do you have any pointers on how to go forward with that or where to look for information on how to accomplish this?

@cfsamson
Copy link
Author

cfsamson commented Oct 21, 2019

I realized that there is no way to do this besides constructing a query yourself and insert batches of 1000 entries and repeat until finished. Maybe this issue should be converted to a feature suggestion?

@cfsamson
Copy link
Author

cfsamson commented Nov 3, 2019

I'll found a pretty nice way of batch inserting which is not limited to 1000 so I'll leave it here for future reference in case it's useful for implementing a batch insert:

fn batch_insert() {
    let mut values: String = (0..10_000).map(|i| format!("({},{}),", i, i * 100)).collect();
    // Remove last comma
    values.pop();
    let update = format!("INSERT INTO sometable(id, price) SELECT id, price FROM (VALUES {})sub(id, price)", values);

    let fut = SqlConnection::connect(CONNSTR).and_then(|conn| {
        conn.simple_exec(update)
    });

    block_on_all(fut).unwrap();

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant