1

I am using a custom MariaDB connector to retrieve some data from a DB.

Whenever I execute a SELECT query, like:

cursor.execute(SOME_SELECT_QUERY)

That returns an object with type: <class 'mariadb.connection.cursor'>

So I tried to convert it to a list with:

results = list(cursor)

So if I print the content of results, this is the result:

[mariadb.Row(record_id=440, chr='chr13'] [mariadb.Row(record_id=449, chr='chr13')]

And I would like to have something like this (a list of dicts):

[{'record_id': 440, 'chr': 'chr13'}, {'record_id': 449, 'chr': 'chr13'}]

Anyone knows how could I get the desired result in a pythonic way?

Cheknov
  • 1,892
  • 6
  • 28
  • 55
  • can you please print the value of ```result = cursor.fetchall() print(result) ``` – Vikash Kumar Apr 29 '21 at 10:18
  • https://mariadb-corporation.github.io/mariadb-connector-python/cursor.html?highlight=row fetchall to get the list of tuples. If you create your cursor with `named_tuple=True`, then you can later convert the named tuples to dicts as follows - https://stackoverflow.com/questions/26180528/convert-a-namedtuple-into-a-dictionary – h4z3 Apr 29 '21 at 10:20
  • Thank you all, but fetchall() didn't work. And named_tuple was already set to True. – Cheknov Apr 29 '21 at 11:07

1 Answers1

1

Did you try to pass dictionary=True while defining the cursor as below?

cursor = conn.cursor(dictionary=True)
Aslı Kök
  • 616
  • 8
  • 19
  • Sadly it doesn't work using the MariaDB connector, only the `named_tuple=True`works but it's not what I need. – Cheknov Apr 29 '21 at 11:03