Skip to content

Is it possible to pass query parameters in a SQL query? #7776

Closed

Description

@shner-elmo

Is your feature request related to a problem or challenge?

I would like to do smth like: execute("SELECT * FROM table WHERE id = ?", &[435]), is it possible?

Describe the solution you'd like

It would be nice to be able to pass the parameters as an array or something similar (instead of hardcoding them into the query-string)

Describe alternatives you've considered

No response

Additional context

No response

Activity

  1. alamb commented on Oct 10, 2023

    @alamb
    Contributor

    hi @shner-elmo -- yes you can do this with PREPARE statements:

    Here are some examples:
    https://github.com/apache/arrow-datafusion/blob/b6f87edbbaaaedd696b2e448fcd7df7ad3cd2c93/datafusion/sqllogictest/test_files/prepare.slt#L18-L20

    You can do it via a DataFrame like this:
    https://docs.rs/datafusion/latest/datafusion/dataframe/struct.DataFrame.html#method.with_param_values

    Something like

    let ctx: SessionContext = ...;
    ctx.sql(""SELECT * FROM table WHERE id = ?")
      .await?
      .with_param_values(vec![lit(435)])?
      .collect()

    This is something that should be documented. I will try and do so

  2. alamb commented on Oct 10, 2023

    @alamb
    Contributor

    PR with documentation: #7785

  3. alamb commented on Oct 10, 2023

    @alamb
    Contributor

    (there are some rough edges around the usability that I fixed in #7785)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions