TypeError:“NoneType”对象没有属性“\uu getitem\uu”,而cur.fetchone不是Non

2024-06-23 02:34:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

cur1.execute("SELECT * FROM consommation WHERE zone=? AND date=?",(zone,date_p))
if cur1.fetchone() is not None:
    abcisses+=(cur1.fetchone()[0][11:13]+'h',)

但是当我运行它时,我有一个错误:TypeError: 'NoneType' object has no attribute '__getitem__',而cur1.fetchone不是空的。所以我不明白为什么不起作用。有人知道为什么吗?


Tags: and代码fromnonezoneexecutedateif
1条回答
网友
1楼 · 发布于 2024-06-23 02:34:15

fetchone获取数据并更改cursor对象的状态,即如果只有一行要获取,则对fetchone的下一个调用将返回None

尝试下一个:

cur1.execute("SELECT * FROM consommation WHERE zone=? AND date=?", (zone, date_p))
res = cur1.fetchone()
if res:
    abcisses += (res[0][11:13] + 'h',)

相关问题 更多 >