views:

278

answers:

2

Hi everyone!

I'm using Python and its MySQLdb module, is it possible to do a "selectmany"-like from a tuple/dictionary/list in the condition

something like this:

cursor.executemany("""SELECT * FROM customers WHERE name= %(name)s""",[d.__dict__ for d in data])

selected_rows = cursor.fecthall()

doing a delete/update/insert works fine with this method :

cursor.executemany("""UPDATE customers SET item = %(item)s WHERE name = %(name)s""",[d.__dict__ for d in data])
+1  A: 

You could use the "WHERE IN" sql syntax, for example,

SELECT * FROM customers WHERE name IN ('john', 'mary', 'jane');
philjohn
+1  A: 

I haven't used the executemany method yet, but I wonder if it's meant to be used for SELECTs. What about

... where name in (...)

instead of

... where name = ...

and inserting a tuple containing the keys of your data dictionaries?

jellybean