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

早过忘川 提交于 2019-12-04 01:26:50

问题


Suppose I query a 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 the column names returned by the query?

I think it is something like this:

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

...but .keys() is not it.

nor .columnNames()


回答1:


cur.description is a read-only attribute containing 7-tuples that look like:

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

So for column names you might do:

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

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




回答2:


There is a columnNames property on the collection of SQLrow objects.

So,

for k in result.columnNames:
    print(k)


来源:https://stackoverflow.com/questions/9752372/how-do-i-get-the-column-names-from-a-row-returned-from-an-adodbapi-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!