Mapping Column Names to results using DBI

Lance Ellinghaus Lance.Ellinghaus at avnet.com
Wed Jan 19 17:51:47 EST 2000


I had the same need and this is what I did. I did my query using the cursor
(cur) and then did the following code.
This code would then return an array of dictionaries. Each dictionary would be
one returned record and each of the
column names would be elements in the dictionary.
I am using the DCOracle module, but it should be pretty similar.

    dsc = cur.description
    l = len(dsc)
    lst = cur.fetchall()
    rtn = []
    for data in lst:
     dict = {}
     for i2 in range(l):
         dict[string.lower(dsc[i2][0])] = data[i2]
     rtn.append(dict)
    return rtn

-------From: Timothy Grant  on 01/19/2000 4:37:22 PM-------

From: Timothy Grant <tjg at avalongroup.net>
To: Python People <python-list at python.org>
cc:
Subject: Mapping Column Names to results using DBI






Hi again,

Right now I have an app that works ok, but I forsee a problem in the
future.

As you know, the results from a database query are returned in a tuple.
I can access those results by position just fine, is it possible to map
the column names onto the results, so I can access columns by name
rather than by number?

While indexing by number works, it is incredibly fragile and will start
failing the minute the underlying DB changes.

Once again, it appears to me from the DBI spec that there should be a
way to do this, I just can't figure it out from the spec.

I'm using mxODBC if that helps.

Thanks.
--
Stand Fast,
    tjg.

Chief Technology Officer              tjg at exceptionalminds.com
Red Hat Certified Engineer            www.exceptionalminds.com
Avalon Technology Group, Inc.                   (503) 246-3630
>>>>>>>>>>>>EXCEPTIONAL MINDS, INNOVATIVE PRODUCTS<<<<<<<<<<<<

--
http://www.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list