Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get column names (title) from a Vertica data base?

Tags:

python

vertica

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:

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)

cheers

like image 587
valenzio Avatar asked Oct 28 '25 08:10

valenzio


1 Answers

Two ways:

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

ColumnList = temp[0].keys()

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

ColumnList = [d.name for d in cur.description]

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

like image 113
woot Avatar answered Oct 29 '25 22:10

woot