博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
cursor.MySQLCursorDict Class
阅读量:7025 次
发布时间:2019-06-28

本文共 1879 字,大约阅读时间需要 6 分钟。

5.9.6.4 cursor.MySQLCursorDict Class

The MySQLCursorDict class inherits from . This class is available as of Connector/Python 2.0.0.

MySQLCursorDict cursor returns each row as a dictionary. The keys for each dictionary object are the column names of the MySQL result.

Example:

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']

The preceding code produces output like this:

Countries in Europe:* Albania* Andorra* Austria* Belgium* Bulgaria...

It may be convenient to pass the dictionary to format() as follows:

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))
           
 User Comments
   Posted by blair gemmer on December 15, 2014
If you want to use stored procedures, please use this format:
cursor.callproc(stored_procedure_name, args)
result = []
for recordset in cursor.stored_results():
for row in recordset:
result.append(dict(zip(recordset.column_names,row)))
 
==============
 

10.5.11 MySQLCursor.column_names Property

Syntax:

sequence = cursor.column_names

This read-only property returns the column names of a result set as sequence of Unicode strings.

The following example shows how to create a dictionary from a tuple containing data with keys using column_names:

cursor.execute("SELECT last_name, first_name, hire_date "               "FROM employees WHERE emp_no = %s", (123,))row = dict(zip(cursor.column_names, cursor.fetchone()))print("{last_name}, {first_name}: {hire_date}".format(row))

Alternatively, as of Connector/Python 2.0.0, you can fetch rows as dictionaries directly; see .

转载于:https://www.cnblogs.com/kungfupanda/p/5937342.html

你可能感兴趣的文章
asp.net系统过滤器、自定义过滤器
查看>>
CSS3 Animation
查看>>
window 下常用的一些命令和应用
查看>>
mysql having的用法
查看>>
重新认识java-忽视的注释
查看>>
Sierpinski三角
查看>>
Dos下查看端口
查看>>
深入探讨Java类加载器
查看>>
Go 开发 HTTP
查看>>
textview的滚动
查看>>
使用JQuery.validate后的bootstrap风格校验提示‏
查看>>
iOS开发中NSTimer的开启与关闭
查看>>
NotePad++中SourceCookifier插件的使用
查看>>
jvm gc日志分析
查看>>
springmvc hello-servlet.xml配置文件
查看>>
kindeditor + syntaxhighlighter 使文章内的插入代码高亮显示
查看>>
基于微博数据用 Python 打造一颗“心”
查看>>
我的Linux发行版/桌面环境选择之路
查看>>
angular2 学习二 [property] - 绑定属性
查看>>
python数据库sqlite基础(一)-------数据库创建,表的建立,数据录入,数据查询...
查看>>