文件首頁
MySQL Connector/Python 開發人員指南
相關文件 下載本手冊
PDF (美式信紙) - 0.7Mb
PDF (A4) - 0.7Mb


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
    ))