Created
December 20, 2013 12:31
-
-
Save h2rd/8054160 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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