cursor.MySQLCursorDict Class

5.9.6.4 cursor.MySQLCursorDict Class

The MySQLCursorDict class inherits from MySQLCursor. 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))

PREV   HOME   UP   NEXT

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 Section 10.6.4, “cursor.MySQLCursorDict Class”.

时间: 2024-08-15 17:56:04

cursor.MySQLCursorDict Class的相关文章

python mysql Connect Pool mysql连接池 (201

 easy_install mysql-connector-python >>>import mysql.connector as conner >>> conn = conner.connect(user="root", passwd="kaimen", db="zentao", port=3306, pool_name = "mypool", pool_size = 3,) >&g

Android sqlite cursor的遍历

查询并获得了cursor对象后,用while(corsor.moveToNext()){}遍历,当corsor.moveToNext()方法调用,如果发现没有对象,会返回false public List<MMImage> getAll() { List<MMImage> list = new ArrayList<MMImage>(); Cursor c = null; try { c = database.query(TABLE, null, null, null,

CSS中cursor的pointer 与 hand

附:cursor属性收集 光标类型   CSS 十字准心 cursor: crosshair; 手 cursor: pointer; cursor: hand; 写两个是为了照顾IE5,它只认hand. 等待/沙漏 cursor: wait; 帮助 cursor: help; 无法释放 cursor: no-drop; 文字/编辑 cursor: text; 可移动对象 cursor: move; 向上改变大小(North)   cursor: n-resize; 向下改变大小(South)  

android 出现Make sure the Cursor is initialized correctly before accessing data from it

Make sure the Cursor is initialized correctly before accessing data from it 详细错误是:java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it. 出现这个原因是因为我在

css js:cursor属性

定义: 规定要显示的光标形状 CSS用法: JS用法 object.style.cursor="crosshair"

游标cursor 与循环fetch

本文部分非原创 使用示例: declare myCursor cursor for select ID,[Column1],[Num] from Table1 open myCursor; declare @ID int,@Column1 varchar(50),@Num int fetch next from myCursor into @ID ,@Column1 ,@Num; while @@FETCH_STATUS = 0 begin --do something fetch next f

SQL Cursor 基本用法

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 table1结构如下 id int name varchar(50) declare @id int declare @name varchar(50) declare cursor1 cursor for --定义游标cursor1 select * from table1 --使用游标的对象(

Oracle PLSQL Demo - 10.For Loop遍历游标[FOR LOOP CURSOR]

declare cursor cur_emp is select t.* from scott.emp t; begin for r_emp in cur_emp loop dbms_output.put_line(r_emp.empno || ' ' || r_emp.sal); end loop; end;

Oracle PLSQL Demo - 08.定义显式游标[Define CURSOR, Open, Fetch, Close CURSOR]

declare v_empno scott.emp.empno%type; v_sal scott.emp.sal%type; cursor cur_emp is select t.empno, t.sal from scott.emp t; begin open cur_emp; loop fetch cur_emp into v_empno, v_sal; exit when cur_emp%notfound; dbms_output.put_line(v_empno || ' ' || v