Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()

like image 459
Cheeso Avatar asked Mar 17 '12 17:03

Cheeso


People also ask

How can I get column names from a table in SQL query?

USE db_name; DESCRIBE table_name; it'll give you column names with the type.

Which query can be used to retrieve the column name?

We can verify the data in the table using the SELECT query as below. We will be using sys. columns to get the column names in a table. It is a system table and used for maintaining column information.

How do I get column names in R?

To access a specific column in a dataframe by name, you use the $ operator in the form df$name where df is the name of the dataframe, and name is the name of the column you are interested in. This operation will then return the column you want as a vector.

How do I get a list of column names in SQL Server?

Column search A feature that can be used to search for column names in SQL Server is Object search. This feature allows users to find all SQL objects containing the specified phrase.


1 Answers

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/

like image 90
mechanical_meat Avatar answered Nov 15 '22 00:11

mechanical_meat