How to extract NaiveDate and NaiveTime values from mySQL results in Rust?

I'm interating over results from mySQL query to generate an array of JSON objects, but some data types need to be serializable.

I've tried just extract the values explicitly by setting NaiveDate and NaiveTime, but I get this error:

Trait `FromValue` is not implemented for `NaiveDate` [E0277]

this is actually the code:

let results: Vec<Row> = conn.exec(
        &stmt,
        params! {
            "date" => date
        },
    )?;
    let mut events = Vec::new();
    for row in results {
        let event = json!({
            "event_id": row.get::<String, _>("event_uuid").unwrap_or_default(),
            "date": row.get::<NaiveDate, _>("date").unwrap_or_default(),
            "time": row.get::<NaiveTime, _>("time").unwrap_or_default(),
        }
        );
        events.push(event)
    }

When you fetch data from MySQL, the client library can only read it into Rust datatypes that have implementations of the FromValue trait.

The client library doesn't provide implementations for types from the chrono crate (NaiveDate, etc), but it does provide them for Time, Date, PrimitiveDateTime and Duration from the time crate.

It would probably be easier to use these types instead.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.