How to pass multiple parameters for a query statement in tokio_postgres?

860 Views Asked by At

I need to pass several parameters into the tokio_postgres::query Statement. How can I properly do this?

The way I do it below doesn't work, what gets passed into the SQL database is the unconverted $2, instead of the date 2021-04-06.

use tokio_postgres::{Error, NoTls};

#[tokio::main]
async fn main() -> Result<(), Error> {
    let (client, connection) = tokio_postgres::connect(
        "dbname=database user=admin password=postgres host=db port=5432",
        NoTls,
    )
    .await?;


    // Spawn the connector in a separate async task
    tokio::spawn(async move {
        if let Err(e) = connection.await {
            eprintln!("connection error: {}", e);
        }
    });

    // How do I pass several parameters correctly here?
    client
        .query(
            "INSERT INTO table_name (url, date_added) \
                    VALUES ('$1', '$2');",
            &[&url, &"2021-04-06"],
        )
        .await?;
}
0

There are 0 best solutions below