Is there any way to get the results from a fetchall() as a dictionary using pymysql?
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.
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.
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.
class pymysql.cursors. DictCursor (connection) A cursor which returns results as a dictionary. class pymysql.cursors.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With