Skip to content
Advertisement

How do i delete a record from SQLite using “name” for example instead of id?

I have a code below which Deletes records from the database using the “email_address” when i use the Id, it deletes fine, but when i switch it to “email_address” it gives me a sqlite3.OperationalError. I have an input field which stores the data that is going to be deleted and is passed down below as “email_delete”. Does anyone know how to fix it?This is the link to the image which contains the error message

def removeEmail():
conn = sqlite3.connect("email.db")
c = conn.cursor()
if c.execute("DELETE from email WHERE email_address =(?)" + email_delete.get()):
    deleted_email = Label(
        root, text="You have been Unsubscribed from the mailing list"
    )
    deleted_email.grid(row=5, column=0)
else:
    error_label = Label(root, text="There is no such record")
    error_label.grid(row=5, column=0)

email_delete.delete(0, END)
conn.commit()
conn.close()

Advertisement

Answer

You need to pass the values that will replace the placeholders in your query as a second argument to c.execute(...) instead of concatenating them together with the query string:

Instead of

c.execute("DELETE from email WHERE email_address =(?)" + email_delete.get())

do

c.execute("DELETE from email WHERE email_address=?", (email_delete.get(),))
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement