我在一个非空的查询结果上获取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]
发布于 2018-07-23 02:49:56
我没有看到@PRMoureu的评论。她/他是完全正确的。这是因为您已经在打印中获取了这两个值。您需要重新执行选择或删除您的打印:
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'>
https://stackoverflow.com/questions/51468087
复制相似问题