0

This thing is working:

my_cursor.execute("SELECT word FROM words WHERE id = 45")

How can I make this thing working?

my_id = 45
my_cursor.execute("SELECT word FROM words WHERE id = my_id")
2
  • Assuming psycopg2 the docs spell it out Passing parameters Commented Aug 15, 2022 at 17:23
  • my_cursor.execute(f"SELECT word FROM words WHERE id = {my_id}") with modern string formatting. Commented Aug 15, 2022 at 18:05

1 Answer 1

2

In Python's Postgres library, you can interpolate values with %s and pass them as secondary arguments to execute.

my_cursor.execute("SELECT word FROM words WHERE id = %s", (my_id,))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.