DB-API: how can I find the column names in a cursor?

A.M alanalan at newsgroup.nospam
Thu Jun 1 09:20:09 EDT 2006


Thank you Fredrik for help.

Would you be able to help with the second part of question:

The other problem is accessing data in each row by column name. In Ruby I
can say:

Print row["ColName"]

In Python; however, I must access to row contents by integer index, like
PRINT ROW[0], which reduces my program's readability.

Can I access to row's contents by column name?

Thanks again,
Alan



"Fredrik Lundh" <fredrik at pythonware.com> wrote in message 
news:mailman.6403.1149166487.27775.python-list at python.org...
> "A.M" wrote:
>
>> The problem is I don't know how to find out what are the column name and 
>> type that comes out of query (each row in cursor).
>>
>> Is there any possibility that my Python code can find out the column name 
>> and type in each row in cursor?
>
>>From "cursor objects" in the DB-API documentation:
>
>    .description
>
>    "This read-only attribute is a sequence of 7-item
>    sequences.  Each of these sequences contains information
>    describing one result column: (name, type_code,
>    display_size, internal_size, precision, scale,
>    null_ok). The first two items (name and type_code) are
>    mandatory, the other five are optional and must be set to
>    None if meaningfull values are not provided."
>
> The full spec is available here: http://www.python.org/dev/peps/pep-0249/
>
> </F>
>
> 





More information about the Python-list mailing list