Create a dictionary in Python by receiving values from the database

Asked

Viewed 719 times

1

After accessing the database and have the desired query in case:

...
c =  conn.cursor()
c.execute('SELECT `tb1`.`Nome`, `tb1`.`Apelido` FROM tb1')

How to create a python dictionary for my command above?

dict = {Nome:'Apelido'} ...
?

1 answer

1


You can try using the.fetchall() cursor, explained here.

It returns a list of tuples with the result of your SELECT. From this it is easy to turn into dictionary.

You can still pass a parameter (Dictionary = True), which for me has not yet worked. See here

Good job!

  • Very good the parameter Dictionary = True. I will use it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.