0
votes

I am trying to delete a row in my SQLite database using the following lines:

conn = sqlite3.connect('test.db')
c = conn.cursor()
memID = str(idBox.get())
#c.execute('DELETE FROM Test WHERE memberID = idBox.get()')

so i am trying to delete from the table using a tkinter entry (idBox) but get the following error:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\Python36\lib\tkinter__init__.py", line 1699, in call return self.func(*args) File "I:/Onedrive/PycharmProjects/TKinter/MainProgram/main.py", line 36, in delete_data_entry c.execute('DELETE FROM Test WHERE memberID = idBox.get()') sqlite3.OperationalError: near "(": syntax error

I have been stuck with this for quite some time and would appreciate some help

more code (memberid variable being created):

memberid = str(uuid.uuid4())
print(memberid[0:8])
c.execute("INSERT INTO Test(memberID, fullname, username, password) VALUES (?, ?, ?, ?)",
          (memberid[0:8], fullnameBox.get(), usernameBox.get(), passwordBox.get()))
conn.commit()
1
did u try just putting a value like 5 to see if it works staticly? - user1767754
Have you tried c.execute('DELETE FROM Test WHERE memberID = ' + memID) ? - Philip B.
Gave it a try and got this: ' Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\Python36\lib\tkinter_init_.py", line 1699, in call return self.func(*args) File "I:/Onedrive/PycharmProjects/TKinter/MainProgram/main.py", line 36, in delete_data_entry c.execute('DELETE FROM Test WHERE memberID = ' + memID) sqlite3.OperationalError: unrecognized token: "44ba195d" ' - Cameron Hamilton
that value is definitely in the database - Cameron Hamilton

1 Answers

0
votes

Considering comment from @Philip Blonde and syntax

c.execute('DELETE FROM Test WHERE memberID = ' + memID)

the problem is that you are passing 44ba195d as memberID and sqlite is expecting something else, maybe an integer. This

DELETE FROM Test WHERE memberID = 44ba195d

will result in if memberID shall be an integer

OperationalError: unrecognited token "44ba195d"

Check what you are getting from idBox.

EDIT: Since you're using uuid as a memberId, you have to change SQL statement to

c.execute("DELETE FROM Test WHERE memberID = '" + memID + "'")

If the data type is not an integer it must be in quotes.

WARNING: Keep in mind that these SQL sentences are vulnerable to SQL injection. You should never use string concatenation (+) or string parameters interpolation (%) to pass variables to a SQL query string. One could input ' OR memberID LIKE '%1% and delete all rows where memberID includes value 1.