可能的解决方案包括将
MySQLCursor此类子类化:
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 Nonedb = mysql.connector.connect(user='root', database='test')cursor = db.cursor(cursor_class=MySQLCursorDict)
现在,该
_row_to_python()方法返回
dictionary而不是
tuple。
我在mysql论坛上找到了它,我相信它是由mysql开发人员自己发布的。我希望他们有朝一日将其添加到mysql连接器包中。
我对此进行了测试,它确实起作用。
更新:正如下面卡尔·MW(Karl
MW)所述。在mysql.connector的v2中不再需要此子类。mysql.connector已经更新,现在您可以使用以下选项启用字典光标。
cursor = db.cursor(dictionary=True)



