Skip to main content

how to get column names in native python client hp_vertica_client

  • November 22, 2017
  • 2 replies
  • 14 views

colin_loghin
Forum|alt.badge.img+2

Does anyone know if the python client they provide
https://my.vertica.com/docs/8.1.x/HTML/python_client/index.html
is able to fetch the column names for a given query programmatically?

Thanks.

2 replies

SergeB
Forum|alt.badge.img
  • Participating Frequently
  • November 28, 2017

Colin, Yes the Python client can give you the number of columns retrieved in a query as well as the description of these columns

Say you execute this query

cursor.execute("SELECT first_name, avg, ops FROM foo WHERE avg > 0.225")

cursor.columns will return 3
cursor.description will return the description (including name) of these 3 columns


colin_loghin
Forum|alt.badge.img+2
  • Author
  • Participating Frequently
  • November 28, 2017

Excellent,
Here is how to get the column names from the active cursor
headers = [desc[0] for desc in cur.description ]
Thanks Serge. The documentation is quite dry on this topic, but i see the cursor attributes mentioned.