jdbc - 杰森 zxJDBC : How to get a dictionary from a cursor?

标签 jdbc jython

我正在使用 JDBC 将我的 jython 连接到一组异构数据库。 使用游标我得到列表形式的行,和游标 还知道元数据 (cursor.description)。

通常您会得到一行作为查询结果的列表:

print resultlist(4)

而且您必须事先知道架构中列的顺序。

我怎样才能得到类似的东西

print resultset[CustomerName]

打印客户姓名?

最佳答案

这个问题的 dict_cursor 怎么样:Django Backend-neutral DictCursor

对我有用的概述(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()

关于jdbc - 杰森 zxJDBC : How to get a dictionary from a cursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6185227/

相关文章:

java - org.apache.jasper.JasperException : An exception occurred processing

java - 使用 Jython 中的 Java 类时,您是否费心使用 getter 和 setter?你应该?

mysql - Jython 和 Mysql - 当前的连接实践是什么?

java - Jython,如何获取脚本所在 jar 的基本路径?

java - 从结果集中创建复杂对象

java - 如何在 ResultSet 中查找记录数

java - 将 java 类添加到 jython 作为 jython 导入模块

grails - 为什么这么多人坚持将 JVM 拖到新的应用程序中?

java - 无法使用 JDBC 正确查询数据库

java - 如何修复c3p0连接池初始化异常?