Getting column names from a cursor using ODBC module?

D

dananrg

Is there any way to retrieve column names from a cursor using the ODBC
module? Or must I, in advance, create a dictionary of column position
and column names for a particular table before I can access column
values by column names? I'd prefer sticking with the ODBC module for
now because it comes standard in Python.

I'm using Python 2.4 at the moment.

Thanks.
 
J

John Machin

Is there any way to retrieve column names from a cursor using the ODBC
module? Or must I, in advance, create a dictionary of column position
and column names for a particular table before I can access column
values by column names? I'd prefer sticking with the ODBC module for
now because it comes standard in Python.

I'm using Python 2.4 at the moment.

Thanks.
 
J

John Machin

Is there any way to retrieve column names from a cursor using the ODBC
module? Or must I, in advance, create a dictionary of column position
and column names for a particular table before I can access column
values by column names? I'd prefer sticking with the ODBC module for
now because it comes standard in Python.

I'm using Python 2.4 at the moment.

Do you mean the odbc module? If so, it doesn't come standard in
Python; it's part of the win32 package.

I haven't used it for years -- my preference on Windows these days
would be mxODBC if the client would pay the licence fee, otherwise
pyodbc. Sorry I'm not answering your question ... perhaps you should
be asking a different question :)

Cheers,
John
 
J

John Machin

Do you mean the odbc module? If so, it doesn't come standard in
Python; it's part of the win32 package.

I haven't used it for years -- my preference on Windows these days
would be mxODBC if the client would pay the licence fee, otherwise
pyodbc. Sorry I'm not answering your question ... perhaps you should
be asking a different question :)

Cheers,
John

But to help you answer your question: if the module that you are using
supports the 2.0 version of the database API (see http://www.python.org/dev/peps/pep-0249/),
then it will support the cursor.description attribute, which gives you
not only the name but the type and 5 other bits of info about each
column. If it doesn't, I'd suggest moving on.

HTH,
John
 
C

Chris

Is there any way to retrieve column names from a cursor using the ODBC
module? Or must I, in advance, create a dictionary of column position
and column names for a particular table before I can access column
values by column names? I'd prefer sticking with the ODBC module for
now because it comes standard in Python.

I'm using Python 2.4 at the moment.

Thanks.

You should be able to do

column_names = [d[0] for d in cursor.description]
 
D

dananrg

Thanks Chris and John. Chris, this worked perfectly with the ODBC
module that ships with Python Win32:
column_names = [d[0] for d in cursor.description]

John, I've never heard of pyodbc but I'll have to look into it.

Thanks again.

Dana
 
M

M.-A. Lemburg

John said:
Do you mean the odbc module? If so, it doesn't come standard in
Python; it's part of the win32 package.

I haven't used it for years -- my preference on Windows these days
would be mxODBC if the client would pay the licence fee, otherwise
pyodbc. Sorry I'm not answering your question ... perhaps you should
be asking a different question :)

mxODBC comes with a cursor.columns() method which allows you to
query column names and many other column specific details of the
database.

See the documentation for details:

http://www.egenix.com/products/python/mxODBC/#Documentation

If you just want to access the result set columns by name,
you can use the cursor.description tuple to map names to
positions.

Please also see the FAQ of the DB API spec reagrding the issues
involved with this approach:

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

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source________________________________________________________________________
2008-07-07: EuroPython 2008, Vilnius, Lithuania

:::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top