title: Python MySQLdb cursor.lastrowid
body: <p>I'm having problems returning auto-incremented ID columns from a MySQL database using MySQLdb python library. 
  I have something like:</p>
  
  <pre><code>sql = """INSERT INTO %s (%s) VALUES (\"%s\")""" %(tbl, colsf, valsf)
  try:
      cursor.execute(sql)
      id = cursor.lastrowid
      db.close()
  except:
      print "Failed to add to MySQL database: \n%s" %sql
      print sys.exc_info()
      db.close()
      exit()
  </code></pre>
  
  <p>However the lastrowid command seems to be returning incorrect values. For instance, I've tried printing out various id columns from the MySQL command line which shows them to be empty, but the lastrowid value keeps increasing by 1 every time the python script is run. Any ideas?</p>