Sqlx only accept string literal, how to use variable?

The codes:==============
// very complex where clause combined from several
let query: String = String.from("where ..........");runtime variables.
error: expected string literal
--> src/abc.rs:80:25
80 | let rows_num: i64 = sqlx::query!(
| ____________^
81 | | &*query,
82 | | ).fetch_one(connection)
| |
= note: this error originates in the macro sqlx::query (in Nightly builds, run with -Z macro-backtrace for more info)
And the doc states:

  • The query must be a string literal or else it cannot be introspected (and thus cannot be dynamic or the result of another macro).

I know the sqlx computes at compile time, my where clause computation is at run time.
I really want to use variable, because the where clause depends other several conditions.
Are there any ways to use variable in sqlx?

Don't use the macro. Instead, use the function that is also called sqlx::query.

Welcome to URLO! Please read the pinned formatting post so that we can more easily read your code and outputs.


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.