id variable in postgres

Question:

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")
Asked By: Pelgoos

||

Answers:

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,))
Answered By: Silvio Mayolo
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.