简体   繁体   English

SQLite 删除查询不起作用?

[英]SQLite delete query not working?

I have the following code to delete a row from a table called posts.我有以下代码从名为 posts 的表中删除一行。 For some reason, the post is not being deleted.由于某种原因,该帖子没有被删除。 I have also tried administering the command manually via command line and it worked just fine.我也尝试通过命令行手动管理命令,它工作得很好。 I also know for sure the post_id is correct and not null as I have also tried passing it back and printing it and the ID shows up correctly.我也确定 post_id 是正确的并且不为空,因为我也尝试过将它传回并打印它并且 ID 正确显示。 I should mention that it does not spit out an error.我应该提到它不会吐出错误。 Just nothing happens.只是什么都没有发生。

def deletePost(post_id):
con = sql.connect("database.db")
cur = con.cursor()
cur.execute("DELETE FROM posts WHERE id = (?)", [post_id])
con.close()

You are not committing, before close commit the changes:在关闭提交更改之前,您没有提交:

Check this.检查这个。

cur.execute("DELETE FROM posts WHERE item = ?", (post_id,))
con.commit()
con.close()

General info for sqlite: sqlite的一般信息:

One import point is, the comma "," in tuple.一个导入点是元组中的逗号“,”。 if you have one item then adding the comma at end is mandatory but if you have more than one entries in tuple(which you are passing to sqlite) then no comma after last item.如果您有一个项目,那么在末尾添加逗号是强制性的,但如果您在元组中有多个条目(您将其传递给 sqlite),则在最后一个项目之后没有逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM