0

using SQL trying to delete a client from a table that is input through a tkinter listbox and I'm getting this error:

TypeError: 'str' object is not callable`

this is the code i'm working with:

class delete_client:
def __init__(self):
    self.window = Tk()
    self.window.wm_protocol("WM_DELETE_WINDOW", self.close)
    self.window.title("Delete Clients")

    self.delete_client_lst = Listbox(self.window, width=50, height=15)
    self.delete_client_lst.grid(row=0, column=0, sticky=W, columnspan=2, padx=10, pady=3)
    self.delete_button = Button(self.window, text='Delete Client', command = self.delete)
    self.delete_button.grid(row=4,column = 2,sticky =E)

    self.surname_lbl = Label(self.window, text='Client Surname')
    self.surname_lbl.grid(row=2, column=0)

    self.surname_entry = Entry(self.window, width=10)
    self.surname_entry.grid(row=2, column=1)


def delete(self):
    given_surname = self.surname_entry.get()

    sql = "DELETE FROM Client"
    sql = "WHERE Surname = %s" (given_surname)
    c.execute(sql)

def close(self):
    self.window.destroy()

thanks

1 Answer 1

2

This line

sql = "WHERE Surname = %s" (given_surname)

should be

sql = "WHERE Surname = %s" % given_surname

or

sql = "WHERE Surname = {}".format(given_surname)

Otherwise it looks like you are trying to call a function off your string object where given_surname is being passed as a function argument.

Sign up to request clarification or add additional context in comments.

4 Comments

That will use string interpolation, so at major risk of injection attacks. Also, the first part of the query will be lost. Just use c.execute("DELETE FROM Client WHERE Surname = ?", (given_surname,)). This will escape malicious input and will also be faster.
Really should not be using string operations here.
so, i used: 'c.execute("DELETE FROM Client WHERE Surname = ?", (given_surname,))' and i'm getting no more errors, but it only seems to be temporarily removing the data from the table. when i close and re-run the program they haven't been deleted...
Because you need conn.commit() to push the changes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.