[Tutor] Building an SQL query

Greg Lindstrom tubaranger at gmail.com
Thu Jun 2 15:48:04 CEST 2005


I am building a query to hit a Postgres (8.0.1) database from Python (4.2.1) 
on Linux. Here's how I've been doing it for the past year or so:

data = {}
data['start_date'] = '2005-6-2'
data['last_name'] = 'Johnson'

query = '''
FROM my_table
WHERE date >= '%(start_date)s'
AND last_name = '%(last_name)s'
''' % data
results = my_database.Execute(query)

and life has been good. What I would like to do now is use the Postgres "IN" 
operator. For example:

ids_to_process = ('1','2','3','5','7','11')

I would like to get something akin to:

query = '''
UPDATE my_table 
SET state = 'processed' 
WHERE id IN ids_to_process

This would, of course, set the 'state' column to 'processed' for all of the 
ids in the list, but can not figure out how to get this into a query to pass 
to the database. Have any of you smart cookies out there dealt with this? 
There are other ways to get the job done, worst case being writing a look 
and issuing an UPDATE for each id, but that is not too elegant, IMHO.

Any help or pointers would be greatly appreciated,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050602/613e1f88/attachment.htm

More information about the Tutor mailing list