#best way to write multiple objects in a DB

3 messages · Page 1 of 1 (latest)

cerulean holly
#

hello folks! i have an array of object, which i want to store in my db.
I am afraid, that the array is large enough that i might run out of connections or cause some other weird issue.
In general, what should be the best approach if i want to add a lot of rows in a table ? i have a function to input a single row as the following:
the following:

async fn write_in_table(db_pool: &SqlitePool, my_struct: &MyStruct) -> Result<()> {
    let mut conn = db_pool.acquire().await?;

    query!(
        r#"
           INSERT INTO structs_table (foo, bar, baz, ..) 
           VALUES (?, ?, ?, ..)
           "#,
        my_struct.a, 
        my_struct.b, 
        my_struct.c, 
        ...
    )
    .execute(&mut conn)
    .await?;

    Ok(())
}

and i am calling this functions for each element in an array of MyStructs. Is this approach fine ?

heavy yoke
#

what library is this?

#

some database libraries have support for pipelining/batching which may make it better