Eth*_*han 5 postgresql rust rust-sqlx
代码:
// 非常复杂的 where 子句由多个运行时变量组合而成。
let query: String = String.from("where ..........");
let rows_num: i64 = sqlx::query!( &*query).fetch_one(connection)
Run Code Online (Sandbox Code Playgroud)
编译器的错误:
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)
Run Code Online (Sandbox Code Playgroud)
该文档指出:
查询必须是字符串文字,否则无法内省(因此不能是动态的或另一个宏的结果)。
我知道 sqlx 在编译时计算,我的 where 子句计算在运行时。我真的很想使用变量,因为 where 子句取决于其他几个条件。有什么方法可以在sqlx中使用变量吗?