0
저는 프로그래밍 초보자이며 SQLite와 Python을 배우면서 약간의 도움을 얻고 싶습니다.Python과 SQLite를 사용하면 데이터베이스의 항목을 변수로 어떻게 읽습니까?
현재 두 개의 열이 포함 된 "Status.db"라는 데이터베이스가 있습니다. 이 열은 "stamp"이고 INT를 입력하고 "messages"는 TEXT를 연속적으로 입력합니다.
특정 스탬프 I 메시지의 인스턴스를 판독하려고 사용하고있는 코드, 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
누군가이 오류의 의미를 명확히 할 수 있는지 궁금해하며 작동하게하려면 어떻게해야하는지 궁금합니다.
미리 감사드립니다.
편집
: 여기 가 작성fetchone()
이 SQL 쿼리는 어떤 결과 행을 반환하지 않습니다 즉,
None
를 반환하는 것을 의미 데이터베이스
@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))
감사합니다 나를 위해 그 지적에 대한 생각도, 나는 여전히 같은 오류 메시지를 얻고 있음을 고정 후. – Synia
@Synia, 데이터베이스에 데이터가 있습니까? (데이터가 있다면'ID '가 데이터베이스에있는'comment'를 호출하고 있습니까?) – huon
@dbaupp 안녕하세요. 나는 데이터베이스에 체크를했고 어떤 이유로 데이터베이스에 할당 된 스탬프에 소수점 6 개가있는 반면, 지나가는 하나는 2 개 뿐이라는 것을 깨달았습니다. 정수형으로 모두 변경 했으므로 이제는 모든 것이 잘 작동합니다. – Synia