[英]Dictionary structure from SQL cursor
Suppose I had a SELECT SQL query and I wanted to return a structure eg for first 3 rows:假设我有一个 SELECT SQL 查询,我想返回一个结构,例如前 3 行:
{
0: {'ColName0': 'Col1RowValue0', 'ColName1': 'Col1RowValue0'},
1: {'ColName0': 'Col0RowValue1', 'ColName1': 'Col1RowValue1'},
2: {'ColName0': 'Col0RowValue2', 'ColName1': 'Col1RowValue2'}
...
}
I get close with the below but I can't get the outer index structure to work: {0:{ },1:{ }}我接近以下内容,但无法使外部索引结构正常工作:{0:{ },1:{ }}
with read_con.cursor() as cur:
cur.execute(DONOR_SELECT)
column_names = [col[0] for col in cur.description]
temp_d = [dict(zip(column_names, row))
for row in cur.fetchall()]
print(temp_d)
cursor is from pyodbc cursor 来自 pyodbc
You need a dict comprehension
with enumerate
你需要一个带有
enumerate
的dict comprehension
temp_d = {i : dict(zip(column_names, row)) for i, row in enumerate(cur.fetchall())}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.