Get column names (title) from a Vertica data base?
<p>I'm trying to extract the column names when pulling data from a vertica data base in python via an sql query. I am using vertica-python 0.6.8. So far I am creating a dictionary of the first line, but I was wondering if there is an easier way of doing it. This is how I am doing it right now:</p>

<pre><code>import vertica_python
import csv
import sys
import ssl
import psycopg2

conn_info = {'host': '****',
             'port': 5433,
             'user': '****',
             'password': '****',
             'database': '****',
             # 10 minutes timeout on queries
             'read_timeout': 600,
             # default throw error on invalid UTF-8 results
             'unicode_error': 'strict',
             # SSL is disabled by default
             'ssl': False}

connection = vertica_python.connect(**conn_info)
cur = connection.cursor('dict')
str = "SELECT * FROM something WHERE something_happens LIMIT 1"
cur.execute(str)
temp = cur.fetchall()
ColumnList = []
for column in temp[0]:
    ColumnList.append(column)
</code></pre>

<p>cheers</p>

<p>Two ways: </p>

<p>First, you can just access the dict's keys if you want the column list, this is basically like what you have, but shorter:</p>

<pre><code>ColumnList = temp[0].keys()
</code></pre>

<p>Second, you can access the cursor's field list, which I think is what you are really looking for: </p>

<pre><code>ColumnList = [d.name for d in cur.description]
</code></pre>

<p>The second one is better because it'll let you see the columns even if the result is empty. </p>

python vertica