Skip to content

Instantly share code, notes, and snippets.

@h2rd
Created December 20, 2013 12:31
Show Gist options
  • Select an option

  • Save h2rd/8054160 to your computer and use it in GitHub Desktop.

Select an option

Save h2rd/8054160 to your computer and use it in GitHub Desktop.
from pprint import pprint
import mysql.connector
class MySQLCursorDict(mysql.connector.cursor.MySQLCursor):
def _row_to_python(self, rowdata, desc=None):
row = super(MySQLCursorDict, self)._row_to_python(rowdata, desc)
if row:
return dict(zip(self.column_names, row))
return None
cnx = mysql.connector.connect(user='root', database='test')
cur = cnx.cursor(cursor_class=MySQLCursorDict)
cur.execute("SELECT c1, c2 FROM t1")
rows = cur.fetchall()
pprint(rows)
cur.close()
cnx.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment