I am working on a rust project where I am working with tokio postgres. I have a table by name of DATA that has two columns "id" and "opt". I have the data hitting the query as two inputs where one is a String and the other is an Option.
Following tokio postges conventions, I am having to call the client with the help of the source code provided below.I am using COALESCE to check if second parameter is NULL on the sql query and this way I am making the SQL query compact.
let rows = client.query("SELECT * FROM DATA WHERE id = $1 AND opt = COALESCE(opt, $2)", &[&input1, &input2)]).await?;
I am getting this error when I perform a cargo build
.
expected `&dyn ToSql + Sync`, found enum `Result`
Can this error be mitigated from Rust?
The result from
client.query
is of typeResult
. Before working with the actual rows, you'll need to retrieve them from theResult
type and deal with any errors that might have occurred.See the Rust Book section or error handling for details on how to properly separate successful from erroneous results.
You can find the full function signature at docs.rs, for example, where you'll find that the return type is of
Result<Vec<Row>, Error>
.