如果我寫cursor=db.cursor(pymysql.cursors.DictCursor)cursor.execute(query)result=cursor.fetchall()print(result)這可以工作并將結(jié)果打印為字典。但是如果我這樣寫cursor=db.cursor(pymysql.cursors.DictCursor)result=cursor.execute(query).fetchall()print(result)它給我錯(cuò)誤:Traceback (most recent call last): File "senti4SDanalyze.py", line 22, in <module> constructMaleResults() File "senti4SDanalyze.py", line 12, in constructMaleResults result=cursor.execute(query).fetchall()AttributeError: 'int' object has no attribute 'fetchall'這是為什么?
1 回答

嚕嚕噠
TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超7個(gè)贊
在對(duì)象上被調(diào)用result=cursor.fetchall()的方法。fetchallcursor
在result=cursor.execute(query).fetchall()方法fetchall中被方法返回的對(duì)象(或本機(jī)類型)上被調(diào)用execute。
為了更好地理解這一點(diǎn),請(qǐng)?zhí)剿鲀?nèi)置的type和dir:
type(cursor)
dir(cursor)
type(cursor.execute(query))
dir(cursor.execute(query))
添加回答
舉報(bào)
0/150
提交
取消