title: mogrify and returning with psycopg2
body: <p>I'm trying to generate automaticatilly queries. I was thinking using executemany, but I need to use returning statement so I did:</p>
  
  <pre><code>def format_bind(cols, rows):
      return '(' + '), ('.join([', '.join(['%s'] * cols)] * rows) + ')'        
  
  def insert_many(table, values, id_column):
                  if not values:
                      return []
  
                  keys = values[0].keys()
                  conn = psycopg2.connect("dbname='db' user='postgres' host='localhost' password='postgres'")
              cursor = conn.cursor()
              query = cursor.mogrify("INSERT INTO {} ({}) VALUES {} RETURNING {}".format(table,
                                                                                           ', '.join(keys),
                                                                                           format_bind(len(keys), len(values)),
                                                                                           id_column),
                                     [tuple(v.values()) for v in values])
              cursor.execute(query)
              return [t[0] for t in (cursor.fetchall())]
  </code></pre>
  
  <p>The problem is when I execute it, I get: <code>error list index out of range</code> Can anybody help me to fix this?</p>