Skip to content
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

Use Appender from a generic struct #408

Open
karlguidob opened this issue Dec 4, 2024 · 0 comments
Open

Use Appender from a generic struct #408

karlguidob opened this issue Dec 4, 2024 · 0 comments

Comments

@karlguidob
Copy link

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])?;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant