gpt4 book ai didi

python - 行作为字典 - pymssql

转载 作者:行者123 更新时间:2023-12-04 12:20:05 25 4
gpt4 key购买 nike

我想在 pymssql 中接收行作为字典。在 python-idle 我跑了:

>>> conn = pymssql.connect(host='192.168.1.3', user='majid', password='123456789', database='GeneralTrafficMonitor', as_dict=True)
>>> cur = conn.cursor()
>>> cur.execute('SELECT TOP 10 * FROM dbo.tblTrafficCounterData')
>>> cur.as_dict
True
>>> for row in cur:
print row['ID']

但它给出:
Traceback (most recent call last):
File "<pyshell#83>", line 2, in <module>
print row['ID']
TypeError: tuple indices must be integers, not str

有人可以帮忙吗?

最佳答案

您需要像这样添加参数 as_dict=True :

    cursor = conn.cursor(as_dict=True)

如果它是结果集中的字段名称,那么您将能够访问 row['id']。

根据以下文档:

https://pythonhosted.org/pymssql/pymssql_examples.html#rows-as-dictionaries

关于python - 行作为字典 - pymssql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com