Skip to content

QueryBuilder, how to combine push_bind() with separated push_bind() in complex query? #3930

Answered by joeydewaal
ginkcode asked this question in Q&A
Discussion options

You must be logged in to vote

It's these calls to push_bind:

query_builder.push_bind("active");
query_builder.push_bind(18);

You push and bind an argument placeholder. That's why there are two extra bind markers (??) in the query.

Not sure why but it looks like the query builder doesn't have a bind method. Here's a solution I came up with.

pub async fn demo_query() -> sqlx::Result<()> {
    let mut arguments = MySqlArguments::default();

    // `add` requires `sqlx::Arguments` to be in scope.
    arguments.add("active").map_err(sqlx::Error::Encode)?;
    arguments.add(18).map_err(sqlx::Error::Encode)?;

    let mut query_builder = QueryBuilder::with_arguments(
        r#"
        SELECT
            *
        FROM users

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by ginkcode
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants