Neat access to db query results

P

Paj

Hi,

I often wish for a really high-level way to access db results, being
willing to forego some efficiency. I came up with a class you can use
like:

cur = db.cursor()
cur.execute('select abc, def from blah')
for row in dbcur_iter(cur):
print row['abc'], row['def']

Here's the class:

class dbcur_iter:
def __init__(self, cur):
self.cur = cur
self.names = [x[0] for x in cur.description]
def __iter__(self):
return self
def next(self):
x = cur.fetchone()
if x is None:
raise StopIteration
return dict(zip(self.names, x))

Hope you find it useful! Do send me any feedback.

Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top