Closed
Description
I've have some issues with a dynamically generated SQL statement and query_raw. My parameters are Vec<String>
.
I tried different variations on how to pass it, and the only solution I found was:
use tokio_postgres::{NoTls, Error};
use tokio_postgres::types::ToSql;
#[tokio::main]
async fn main() -> Result<(), Error> {
let (client, connection) = tokio_postgres::connect("<redacted>", NoTls).await?;
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
});
// The explicit type declaration is very important, Box<String> will not work
let params: Vec<Box<dyn ToSql>> = vec![Box::new("param".to_owned())];
client
.query_raw("SELECT $1::TEXT", params.iter().map(|x| x.as_ref()))
.await?;
Ok(())
}
Is there a better way?
Metadata
Metadata
Assignees
Labels
No labels