title: psycopg2: Update multiple rows in a table with values from a tuple of tuples
body: <p>I'm attempting to update several rows at once using a tuple of tuples.
  I figured out how to construct the sql statement from <a href="https://stackoverflow.com/questions/18797608/update-multiple-rows-in-same-query-using-postgresql">this post</a>, but implementing it in <code>psycopg2</code> has proven to be more challenging.
  Here's what I have:</p>
  
  <pre><code>c = db.cursor()
  
  new_values = (("Richard",29),("Ronald",30))
  
  sql = """UPDATE my_table AS t 
           SET name = e.name 
           FROM (VALUES %s) AS e(name, id) 
           WHERE e.id = t.id;"""
  
  c.execute(sql, (new_values,))
  </code></pre>
  
  <p>The result is an error: <code>ProgrammingError: table "e" has 1 columns available but 2 columns specified</code>
  This is because the <code>FROM</code> clause is being interpreted as:</p>
  
  <pre><code>FROM (VALUES (("Richard",29),("Ronald",30)))
  </code></pre>
  
  <p>instead of:</p>
  
  <pre><code>FROM (VALUES ("Richard",29),("Ronald",30))
  </code></pre>
  
  <p>I can work around this by doing the following but it seems unsafe:</p>
  
  <pre><code>import re
  c = db.cursor()
  
  sql = """UPDATE my_table AS t 
           SET name = e.name 
           FROM (VALUES %s) AS e(name, id) 
           WHERE e.id = t.id;"""
  sql = c.mogrify(sql, (new_values,))
  
  # Replace the first occurance of '((' with '('.
  sql = sql.replace('((', '(',1)
  
  # Replace the last occurance of '))' with ')'.
  sql = re.sub(r'(.*)\)\)', r'\1)', sql)
  
  sql = c.execute(sql)
  </code></pre>
  
  <p>Is there a better way to do this?</p>