我想在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有人能帮帮忙吗?
发布于 2012-04-02 16:24:24
查看您正在使用的pymssql的版本。只有since 1.0.2才会返回字典,而早期版本似乎都会返回元组。
发布于 2019-08-21 18:30:35
您需要像这样添加参数as_dict=True:
cursor = conn.cursor(as_dict=True)然后,如果行‘is’是结果集中的字段名,则可以访问它。
根据以下文档:
https://pythonhosted.org/pymssql/pymssql_examples.html#rows-as-dictionaries
发布于 2021-08-10 13:04:51
可以在创建连接本身时设置as_dict=True。
pymssql.connect(server='.', user='', password='', database='', as_dict=True,)https://pythonhosted.org/pymssql/ref/pymssql.html?highlight=connect#pymssql.connect
https://stackoverflow.com/questions/9972944
复制相似问题