duckdb-rs icon indicating copy to clipboard operation
duckdb-rs copied to clipboard

Use Appender from a generic struct

Open karlguidob opened this issue 1 year ago • 0 comments

Hi, what is the recommended way to insert records with mixed types? The docs suggest using Appender or do I just go back to plain old SQL inserts?

   struct Person {
        first_name: String,
        last_name: String,
        dob: NaiveDate,
    }

    let db = Connection::open_in_memory()?;

    db.execute_batch("CREATE TABLE foo(first_name VARCHAR, last_name VARCHAR, dob DATE);")?;

    let person1 = Person {
        first_name: String::from("John"),
        last_name: String::from("Smith"),
        dob: NaiveDate::from_ymd_opt(1970, 1, 1).unwrap(),
    };

    let person2 = Person {
        first_name: String::from("Jane"),
        last_name: String::from("Smith"),
        dob: NaiveDate::from_ymd_opt(1975, 1, 1).unwrap(),
    };

    {
        let mut app = db.appender("foo")?;

        // Something like this ...
        app.append_rows([person1, person2])?;
    }

karlguidob avatar Dec 04 '24 10:12 karlguidob