Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

row_factory alternative for mysql in python

I used row_factory in sqllite so that my rows can be returned in a dictionary format with column names. I used:

conn = sqlite3.connect('gutendex.db')
conn.row_factory = dict_factory

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

I want to use similar thing in mysql db as well but I couldnt find an alternative for it

like image 446
EXODIA Avatar asked Apr 26 '26 00:04

EXODIA


1 Answers

Since this question does not has any answer and I solved the problem using the comment by Barmar I am pasting that as accepted solution.

The dictionary=True parameter to connection.cursor() tells it to return rows as dictionaries

like image 166
EXODIA Avatar answered Apr 27 '26 13:04

EXODIA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!