Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 0.45 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Parameterized queries with psycopg2: SELECT from a Python list
  2. >>> lst2
  3. [{'id': 97600167}, {'id': 97600168}, {'id': 97611194}]
  4. >>> cur.executemany("SELECT id, parent_id FROM my_table WHERE id=%(id)s", lst2)
  5. >>> cur.fetchall()
  6. [(97611194, 10020688), (None, None), (None, None)]
  7.        
  8. >>> lst2
  9. [{'id': 97600167}, {'id': 97600168}, {'id': 97611194}]
  10. >>> ids = tuple(x['id'] for x in lst2)
  11. >>> cur.execute("SELECT id, parent_id FROM my_table where id in %s",[ids])