1

I have a query that I want to append to a file (to generate a list of queries) that will be executed later.

location_filter="SELECT id FROM developers WHERE location='%s'"
out_file.write((location_filter, (location)))

But the output that gets written can't be executed in pgadmin:

("SELECT id FROM developers WHERE location='%s'", u'Seattle')

How can I safely generate this statement?

1 Answer 1

2
conn = psycopg2.connect(database='cpn')
cur = conn.cursor()

location_filter = "SELECT id FROM developers WHERE location = %s"
out_file.write(cur.mogrify(location_filter, (location,)))
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.