-
Notifications
You must be signed in to change notification settings - Fork 1.4k
How to write dynamic query? #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is shown in the third and fifth examples in the Querying section in the README: https://github.com/launchbadge/sqlx#querying let mut cursor = sqlx::query("SELECT * FROM users WHERE email = ?")
.bind(email)
.fetch(&mut conn).await?;
while let Some(row) = cursor.next().await? {
// map the row into a user-defined domain type
} #[derive(sqlx::FromRow)]
struct User { name: String, id: i64 }
let mut stream = sqlx::query_as::<_, User>("SELECT * FROM users WHERE email = ? OR name = ?")
.bind(user_email)
.bind(user_name)
.fetch(&mut conn); The These examples could stand to be copied to the documentation, however. |
one error it give "no method fetch" |
In the error message it should have suggested importing one of the following (depending on what type your database connection is):
These are workarounds for a compiler bug/missing feature and will be removed in 0.4. |
I want to dynamically add it to the SQL based on whether the field value of the object is None
how do I code |
We're discussing that in #291 |
@abonander , would you have an idea on how to insert a statement like the following into the WHERE clause? The purpose of the following query string is to run a wildcard like keyword search in a JSONB field (POSTGRES). So, the string 'ABC' is searched within the whole 'column_x'. This runs without an error:
Numerous different attempts to build the above with Sqlx have failed. As the final resort, the following compiles with query_as!, only to end up with the run-time error given by the DB.
where I provide $1 as the following string literal:
The error given by the DB:
By the way, in the DB tool I use (DBeaver) I can do this, that is, provide the whole raw string starting with the method jsonb_path_exists(... as a single bind variable. |
If you do not use Macros and use sql to splice it into a string, it is easy to generate "SQL Injection Attack "
The text was updated successfully, but these errors were encountered: