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

Daniel Dittmar daniel.dittmar at sap.corp
Thu Jun 1 09:54:00 EDT 2006


A.M wrote:
> for row in out_cur:
> 
>      print row
> 
[...]

> 
> The other problem is accessing data in each row by column name. 

One useful technique is
for col1, col2, col3 in out_cur:
     sum = sum + col3

Access is still by index, but your code uses ordinary Python variables.
It works better with SELECTs as you can choose the output columns in the 
SQL. With stored procedures, there's always the possibility of someone 
changing the structure of the result set.

There exists a general library for your solution: 
http://opensource.theopalgroup.com/

Daniel





More information about the Python-list mailing list