我是一名编程初学者,希望在学习SQLite和Python方面获得一些帮助。
目前,我有一个名为" Status.db"的数据库。其中包含两列。这些列是" stamp",类型INT,"消息",连续输入TEXT。
我正在使用的代码尝试将一个消息实例(带有特定的标记ID)读入变量"输出"如下:
@cherrypy.expose
def comment(self, ID = None):
con = lite.connect('static/database/Status.db')
with con:
cur = con.cursor()
cur.execute("SELECT messages FROM Status WHERE stamp is ?", (ID,))
temp = cur.fetchone()
output = temp[0]
但是,当我执行此代码时,我收到一条错误消息:
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/cherrypy/_cprequest.py", line 606, in respond
cherrypy.response.body = self.handler()
File "/usr/lib/pymodules/python2.7/cherrypy/_cpdispatch.py", line 25, in __call__
return self.callable(*self.args, **self.kwargs)
File "proj1base.py", line 176, in comment
output = temp[0]
TypeError: 'NoneType' object is not subscriptable
我想知道是否有人可以向我澄清这个错误意味着什么,以及我能做些什么才能让它发挥作用。
提前致谢!
编辑: 这是创建和写入数据库的代码
@cherrypy.expose
def writeStatus(self, newStatus=None, post=None):
"""Update the status file with the most recent status update
by inserting a timestamp and new status into a database
"""
con = lite.connect('static/database/Status.db')
stamp = time()
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Status(stamp INT, messages TEXT)")
cur.execute("INSERT INTO Status VALUES(?, ?)", (stamp, newStatus))
答案 0 :(得分:2)
这意味着fetchone()
正在返回None
,这意味着您的SQL查询未返回任何结果行。
这可能是因为您应该在SQL中使用=
而不是is
:
SELECT messages FROM Status WHERE stamp = ?