Python - Retrieve BD information by column name

Asked

Viewed 405 times

0

Dear, How to retrieve database information by column names and not by indexes?

cursor.execute('SELECT * FROM [AutomacaoCampanhasDashboard].[dbo].[TBL_GCCP2]')
row = cursor.fetchall()
nome = row[0][1]
endereco = row[1][2]

print(nome)
print(endereco)

I’d like to retrieve the information in that way:

nome = row[Nome]
endereco = row[endereco]

print(nome)
print(endereco)
  • Thinking about helping people using mysql, this only works if: crs = Conn.cursor(named_tuple=True) Ref.: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqursornamedtuple.html

1 answer

1

I found out... very simple... Only put . (dot) and column name.

print(row.RESPONSAVEL_TELEFONICA_RA, row.SEGMENTO, row.BENEFICIO_FUTURO)

Browser other questions tagged

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