10.6.4 cursor.MySQLCursorDict Класс
Класс MySQLCursorDict наследуется от MySQLCursor. Этот класс доступен начиная с Connector/Python 2.0.0.
Курсор MySQLCursorDict возвращает каждую строку в виде словаря. Ключами каждого объекта словаря являются имена столбцов результата MySQL.
Пример:
cnx = mysql.connector.connect(database='world')
cursor = cnx.cursor(dictionary=True)
cursor.execute("SELECT * FROM country WHERE Continent = 'Europe'")
print("Countries in Europe:")
for row in cursor:
print("* {Name}".format(Name=row['Name']
Представленный код генерирует вывод, похожий на этот:
Countries in Europe:
* Albania
* Andorra
* Austria
* Belgium
* Bulgaria
...
Может быть удобно передать словарь в format() следующим образом:
cursor.execute("SELECT Name, Population FROM country WHERE Continent = 'Europe'")
print("Countries in Europe with population:")
for row in cursor:
print("* {Name}: {Population}".format(**row))
© 2025 Oracle
Licensed under the GPLv2 License.