Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite delete query not working?

I have the following code to delete a row from a table called 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. 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()
like image 594
CNorlander Avatar asked Feb 01 '26 10:02

CNorlander


1 Answers

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:

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.

like image 164
Muhammad Haseeb Khan Avatar answered Feb 03 '26 00:02

Muhammad Haseeb Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!