How do I get column names from a string returned from an adodbapi query?

Suppose I am querying the database like this:

import adodbapi
conn = adodbapi.connect(connStr)
tablename = "[salesLT].[Customer]"

cur = conn.cursor()

sql = "select * from %s" % tablename
cur.execute(sql)

result = cur.fetchall()

      

The result is, I think, a sequence of SQLrow objects.

How can I get a list or sequence of column names returned by a query?

I think it is something like this:

    row = result[0]
    for k in row.keys():
        print(k)

      

... but is .keys()

n't it.

and .columnNames()

+3


source to share


2 answers


cur.description

is a read-only attribute containing 7 tuples that look like this:

(name, 
type_code, 
display_size,
internal_size, 
precision, 
scale, 
null_ok)

So, for the column names, you can:



col_names = [i[0] for i in cur.description]

      

Link: http://www.python.org/dev/peps/pep-0249/

+11


source


The collection of SQLrow objects has a property called columnNames.

So,



for k in result.columnNames:
    print(k)

      

0


source







All Articles