gpt4 book ai didi

Python: 'NoneType' 对象不可在非空查询结果上订阅

转载 作者:行者123 更新时间:2023-12-01 09:13:17 25 4
gpt4 key购买 nike

我得到object is not subscriptable在非空查询结果上。当我打印时 c.fetchone()它打印了正确的结果,但是当我检查类型时它显示 <class 'NoneType'>

import sqlite3

conn = sqlite3.connect("scraping.db")
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS Terminate (numar INT, arj INT )')
conn.commit()
c.execute('DELETE FROM Terminate')
conn.commit()
c.execute('INSERT OR IGNORE INTO Terminate ( numar, arj ) VALUES (1,0)')
conn.commit()
c.execute("SELECT * FROM Terminate") # ORDER BY numar DESC LIMIT 1")
print(c.fetchone(), type(c.fetchone()))

ins = c.fetchone()[0]

最佳答案

我没有看到@PRMoureu 评论。她/他是完全正确的。发生这种情况是因为您已经在打印中获取了两个值。您需要重新执行 SELECT 或删除您的打印:

import sqlite3

conn = sqlite3.connect("scraping.db")
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS Terminate (numar INT, arj INT )')
conn.commit()
c.execute('DELETE FROM Terminate')
conn.commit()
c.execute('INSERT OR IGNORE INTO Terminate ( numar, arj ) VALUES (1,0)')
conn.commit()
c.execute("SELECT * FROM Terminate") # ORDER BY numar DESC LIMIT 1")


ins = c.fetchone()[0]
print ins, type(ins)

输出看起来没问题

1 <type 'int'>

关于Python: 'NoneType' 对象不可在非空查询结果上订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51468087/

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