如何使 cx-oracle 将查询结果绑定到字典而不是元组?
这是我的代码。我想找到一种方法将查询结果作为字典列表而不是元组列表返回。看起来 cx_oracle 通过部分文档讨论“绑定”来支持这一点。虽然我不知道它是如何工作的。
def connect():
dsn = cx_Oracle.makedsn("host", 1521, "sid")
orcl = cx_Oracle.connect('scott/tiger@' + dsn)
curs = orcl.cursor()
sql = "select * from sometable"
curs.execute(sql)
result = curs.fetchall()
for row in result:
print row[13] #CATEGORY field order
print row['CATEGORY'] # <- I want this to work ('CATEGORY' is the name of a field in the 'sometable' table)
curs.close()
Here is my code. I would like to find a way to have results from a query returned as a list of dictionaries rather than list of tuples. It seems like cx_oracle supports this with parts of the documentation talking about 'binding'. Though I can't figure out how it works.
def connect():
dsn = cx_Oracle.makedsn("host", 1521, "sid")
orcl = cx_Oracle.connect('scott/tiger@' + dsn)
curs = orcl.cursor()
sql = "select * from sometable"
curs.execute(sql)
result = curs.fetchall()
for row in result:
print row[13] #CATEGORY field order
print row['CATEGORY'] # <- I want this to work ('CATEGORY' is the name of a field in the 'sometable' table)
curs.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Bindvars 用于执行查询,例如
按名称(给定命名参数)
将打印:['BOOKID']
按给定值列表的位置
要获得您期望的结果,您可以尝试类似的操作:
Bindvars are used to execute query such as
By name(given named parameters)
will print : ['BOOKID']
by position given a list of values
To get what you expected you could try something like that:
这是一个快速而肮脏的过程。感觉发帖有更好的方法。
Here is a quick and dirty. Feel post a better way.