Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymysql fetchall() results as dictionary?

Tags:

python

mysql

Is there any way to get the results from a fetchall() as a dictionary using pymysql?

like image 957
Ricky Avatar asked Feb 09 '11 02:02

Ricky


People also ask

What does PyMySQL fetchAll return?

PyMySQL fetchAll The fetchAll method retrieves all (remaining) rows of a query result, returning them as a sequence of sequences. In the example, we retrieve all cities from the database table. This SQL statement selects all data from the cities table.

What does cursor fetchAll return?

fetchall() Method. The method fetches all (or all remaining) rows of a query result set and returns a list of tuples. If no more rows are available, it returns an empty list. You must fetch all rows for the current query before executing new statements using the same connection.

What does PyMySQL do?

PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb.

What is PyMySQL cursors DictCursor?

class pymysql.cursors. DictCursor (connection) A cursor which returns results as a dictionary. class pymysql.cursors.


1 Answers

PyMySQL includes a DictCursor. It does what I think you want. Here's how to use it:

import pymysql connection = pymysql.connect(db="test") cursor = connection.cursor(pymysql.cursors.DictCursor) cursor.execute("SELECT ...") 

https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/tests/test_DictCursor.py

like image 160
Seun Osewa Avatar answered Sep 23 '22 22:09

Seun Osewa