1

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])

3 Answers3

3

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

SELECT * FROM customers WHERE name IN ('john', 'mary', 'jane');
philjohn
  • 546
  • 3
  • 9
2

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?

Johannes Charra
  • 29,455
  • 6
  • 42
  • 51
0

executemany for the most part is for insertion or update. Doesn't really work well for querying data, from my experience it will only return the last set of parameters.

Victor Uriarte
  • 479
  • 4
  • 9