2012-04-02 36 views
0

我想在pymssql中以字典的形式接收行。在python空閒,我跑:行作爲詞典 - pymssql

>>> 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 

有人能幫忙嗎?

+1

也許__iter __()是as_dict支持的剩餘部分,請嘗試與cur.fetchall()中的row相同,因爲文檔是顯式的,fetchall()支持as_dict。這聽起來有點愚蠢,但你永遠不知道...... – jpic 2012-04-02 08:25:55

回答

1

查看您正在使用的pymssql的版本。只有since 1.0.2它返回一個字典,早期版本似乎返回元組。