0

I'd like to be able to get the column names of my query back from the database so I can then use it to dynamically set the column length and name of my gui table.

So far after establishing a connection I've tried:

mycursor = mydb.cursor()
mycursor.callproc('db1.load_table', args)
for r in mycursor.stored_results():
    result = r.fetchall()
column_names = mycursor.column_names

I've also tried using mycursor.description

It didn't work.

What's the right approach here?

eshirvana
  • 23,227
  • 3
  • 22
  • 38
daniel ajj
  • 83
  • 9
  • Does this answer your question? [How do I get a list of column names from a psycopg2 cursor?](https://stackoverflow.com/questions/10252247/how-do-i-get-a-list-of-column-names-from-a-psycopg2-cursor) and/or https://stackoverflow.com/questions/47281931/how-to-get-columns-name-in-mysqldb-with-a-python-2-7/62993856 – JonSG Feb 03 '22 at 15:05

1 Answers1

0

Something like this you can :

column_names = [desc[0] for desc in mycursor.description]
eshirvana
  • 23,227
  • 3
  • 22
  • 38
  • I tried this - this is what I got `['@_db1.load_table_arg1']` as column names. I'm using Microsoft Azure's MySQL service, not sure if that changes anything. – daniel ajj Feb 04 '22 at 08:50
  • @danielajj what is your proc returning ,seems like its returning a scalar variable called `load_table_arg1` – eshirvana Feb 04 '22 at 13:57
  • `mycursor.stored_results` returns a tuple of data from the database after running the stored procedure – daniel ajj Feb 04 '22 at 14:08