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

Luis M. González luismgz at gmail.com
Thu Jun 1 09:59:53 EDT 2006


I don't know if it works this way with Oracle, but the python dbpai has
the cursor.description method that can help. For example:

cur.execute( "your query here" )
columns = [i[0] for i in cur.description]

cur.description gives a lot of data about your recordset, and the first
field is the column name.

Hope this helps...
Luis


A.M wrote:
> Hi
>
>
>
> I use a code similar to this to retrieve data from Oracle database:
>
>
>
> import cx_Oracle
>
>
>
> con = cx_Oracle.connect("me/secret at tns")
>
> cur = con.cursor()
>
> outcur = con.cursor()
>
> cur.execute("""
>
>      BEGIN
>
>          MyPkg.MyProc(:cur);
>
>      END;""", cur=outcur)
>
>
>
> for row in out_cur:
>
>      print row
>
>
>
>
>
> 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?
>
>
>
> 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?
> 
> 
> 
> Any help would be appreciated,
> 
> Alan




More information about the Python-list mailing list