10.6.6 cursor.MySQLCursorNamedTuple 类
该类MySQLCursorNamedTuple
继承自
MySQLCursor
. 此类自 Connector/Python 2.0.0 起可用。
游标将MySQLCursorNamedTuple
每一行作为命名元组返回。每个命名元组对象的属性是 MySQL 结果的列名。
例子:
cnx = mysql.connector.connect(database='world')
cursor = cnx.cursor(named_tuple=True)
cursor.execute("SELECT * FROM country WHERE Continent = 'Europe'")
print("Countries in Europe with population:")
for row in cursor:
print("* {Name}: {Population}".format(
Name=row.Name,
Population=row.Population
))