Python 3 + tkinter和sqlite3
我正在做一个模拟应用程序来保存sqlite中的一段文本
这是功能:
def saveNote(self,note_id):
conn = db.connect(fname)
c = conn.cursor()
safeTitle=self.newNoteTitle.get()
safeContents=self.newNoteText.get("1.0",tk.END)
safeLink=self.newNoteLink.get()
safeRemarks=self.newNoteRemarks.get()
conn.execute('UPDATE notes SET (title,contents,remarks,link,created,last_modified,notebook_id) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?) WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, 1))
conn.commit()
self.master.destroy()
执行该功能时,我收到此错误:
conn.execute('UPDATE notes SET (title,contents,remarks,link,created,last_modified,notebook_id) VALUES (?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, ?) WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, 1))
sqlite3.OperationalError: near "(": syntax error
我不太明白为什么语法错了...(我是python的新手)...谁能帮我发现错误?
由于
答案 0 :(得分:2)
我认为您的SQL语句不正确。根据您的解释,您希望将数据插入到表中,因此您希望实际使用INSERT语句而不是UPDATE。我想你可能想要这样做:
INSERT INTO notes(title,contents,remarks,link,created,last_modified,notebook_id)
VALUES(INSERT_THE_VALUES_YOU_WANT_TO_INSERT_HERE)
如果您实际上想要更新现有数据,那么您的语法应如下所示:
只需使用您要设置的值填充""
UPDATE notes
SET
title = "",
contents = "",
remarkts = "",
link = "",
created = "",
last_modified = "",
notebook_id = ""
WHERE notes_id=5
要删除与我提供的查询的任何混淆,您希望在Python中构建查询,如下所示:
conn.execute('UPDATE notes SET title=?,contents=?,remarks=?,link=?,created=?,last_modified=?,notebook_id=?
WHERE notes_id=5', (safeTitle, safeContents, safeRemarks, safeLink, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 1))