3

I am using JDBC to connect my jython to a heterogeneous set of databases. Using a cursor I get the rows in list form, and the cursor also knows the metadata (cursor.description).

Usually you get a row as list as result of a query:

print resultlist(4)

And you have to know the order of the Columns in the schema beforehand.

How can I get something like

print resultset[CustomerName]

to print the name of a customer?

AndreasT
  • 9,417
  • 11
  • 46
  • 60

1 Answers1

2

How about dict_cursor from this question: Django Backend-neutral DictCursor?

Outline of what works for me (Jython 2.5.2):

def dict_cursor(cursor):
    description = [x[0] for x in cursor.description]
    for row in cursor:
        yield dict(zip(description, row))

conn = zxJDBC.connect(db, user, pwd, driver)
cursor = conn.cursor()

query = "..."
cursor.execute(query)

dc = dict_cursor(cursor)
for d in dc:
    print d["SomeColumnName"]
    ...

cursor.close()
conn.close()
Community
  • 1
  • 1
mzjn
  • 48,958
  • 13
  • 128
  • 248
  • Wow a needy one. You are gonna get disappointed a lot. But not by me. I have learnt something from your answer so +1. Have to test it before accepting it, though. Thx for the reminder. – AndreasT Aug 25 '11 at 08:31
  • Wow, a snarky one. Thank you. – mzjn Aug 25 '11 at 16:23