Skip to content

Iterating over the rows with mapping example #114

Closed
@waynenilsen

Description

@waynenilsen

Would it be possible to add an example to the readme? I have a similar example to the one from the readme but cannot seem to figure out how to do the same thing but get the results into a vector using iterator mapping.

So, this works

        let conn = get_connection();
        let stmt = conn.prepare("SELECT id, firstname, lastname FROM person").unwrap();
        for row in stmt.query(&[]).unwrap() {
            let person = Person {
                firstname: row.get(1),
                lastname: row.get(2),
            };
            println!("Found person {}", person.firstname);
        }

But I have to specify the type of row during the mapping which seems like it should be Option<Row<'a> it seems.

        get_connection()
            .prepare("SELECT id, firstname, lastname FROM person").unwrap()
            .query(&[]).unwrap()
            .iter().map( | row | {
                Person {
                    firstname: row.get(1),
                    lastname: row.get(2),
                }
        }).collect().to_json()

I feel like I'm close, any suggestions? The code above does not work but it "feels" pretty close.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions