Архив метки: NaiveDateTime

Rust — подготовленные запросы в PostgreSQL с указанием типа

cargo.toml


[dependencies]
postgres = { version = "0.19.9", features = ["with-uuid-1", "with-chrono-0_4"] }
uuid = { version = "1.11.0", features = ["v4"] }
chrono = "0.4.38"

main.rs

use postgres::{Client, NoTls};
use std::error::Error;
use uuid::Uuid;
use chrono::{NaiveDateTime};


fn main() -> Result<(), Box<dyn Error>> {
    let mut conn = Client::connect(
        "postgresql://postgres:jopa@localhost:5432/postgres",
        NoTls)
    .unwrap();

    let stuff_id = Uuid::new_v4();
    let date_str = "2020-04-12 22:10:57";
    let naive_datetime = NaiveDateTime::parse_from_str(date_str, "%Y-%m-%d %H:%M:%S").unwrap();

    conn.execute(
        "insert into test (stuff_id, naive_datetime) values ($1, $2)",
        &[&stuff_id, &naive_datetime],
    )?;

    Ok(())
}