odbc and accessing database results by field

George Kinney gk_2345 at yahoo.com
Tue Oct 28 19:32:08 EST 2003


"JZ" <jroznfgre at jngpugbjreQBGbet.cy> wrote in message
news:hc2tpvkira068f3n8emok8g6jtsg57gdas at 4ax.com...
> import odbc
> conn = odbc.odbc('mydsn')
> c = conn.cursor()
> c.execute('SELECT TOP 10 field1, field2 FROM TABLE')
> rows = c.fetchall()
> c.close()
> conn.close()
>
> I am using MSSQL2000. My problem is:
> Instead of row[0], rows[1]
> I would like to use row['field1], row['field']
> Is it possible for odbc module?
> I cannot find any comprehensive documentation :(

Well, I've been using oledb, but it should be fairly similar.

I just wrote a small wrapper class for the ADODB.Recordset object, that when
its Open() method is called, (as well as MoveNext, MovePrev, etc.) it copies
the current field names and their values into self.__dict__, so I can access
them via r.Field1, r.Field2, etc.

Another route I've gone (since OS/400 allows field names like TEST#REC@,
which python pukes on, is to over-ride __call__ to handle field data. (i.e.
c('test') = somevalue, etc.).

Anyways, a couple of ideas.






More information about the Python-list mailing list