6

I am working on a proof of concept, using Python and Duckdb.

I am wanting to use a variable\parameter inside the Duckdb SELECT statement.

For example,

y = 2
dk.query("SELECT * FROM DF WHERE x > y").to_df()

How can y be properly referenced?

I was not able to find any documentation\reference @ web.

3
  • 1
    Use an f string Commented Dec 10, 2022 at 21:15
  • y is a scalar value? and afterwards you make it a dataframe? Commented Dec 10, 2022 at 21:20
  • 1
    dk.query(f"SELECT * FROM DF WHERE x > {y}").to_df() Commented Dec 10, 2022 at 21:32

1 Answer 1

6

Your query looks suspicious to me; you always should use a prepared statement to pass variables to the database. It will prevent sql injection and so increase security:

y = 2
con.execute("SELECT * FROM DF WHERE x > ?", [y]).df()
Sign up to request clarification or add additional context in comments.

6 Comments

stackoverflow combined 2 lines when i submitted...
sammywemmy answered it but don't see how to indicate. thanks!
it is insecure so try mine
prepare statements require prepare query syntax? duckdb.org/docs/sql/query_syntax/prepared_statements.html
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.