Save fields back to python variables

Asked

Viewed 164 times

0

I made a query in mysql with python and show the result:

cursor = db.cursor()    
cursor.execute("SELECT Dias, HoraConsulta, HoraSaida, nome, Consulta, centrodb.LocalConsulta.Descricao, Contato FROM centrodb.RegistoConsultas LEFT OUTER JOIN centrodb.LocalConsulta ON centrodb.LocalConsulta.Id = centrodb.RegistoConsultas.`Local` LEFT OUTER JOIN centrodb.utentes ON centrodb.utentes.codigoutente = centrodb.RegistoConsultas.Utente LEFT OUTER JOIN centrodb.DiasSemana ON centrodb.DiasSemana.Id = centrodb.RegistoConsultas.DiaSemana")    
myresult = cursor.fetchone()    
print(myresult)

Instead of print, I intend to store each column returned in the python query in variables

  • Which is the way out of print? What type is the object myresult? A dictionary?

  • @Anderson Carlos Woss, the print was just to print the line as a test. Now my question is how to store the data returned from each column in variables

  • What about the type of the object? It’s an actual dictionary?

1 answer

1


In accordance with the documentation, the return of fetchone will be a tuple with the values returned from the database. So, to create variables, you can deconstruct the tuple:

dias, hora_consulta, hora_saida, ... = myresult

Thus, each tuple value will be stored in the respective variable.

Browser other questions tagged

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