-1

I built this program and can't seem to figure out how to remove all of the elements from the database. My code is below:

def clear_books():
c = cursor()
with c.connection:
    for book in c.execute("SELECT * FROM books"):
        c.execute("DELETE FROM books WHERE title=?", (book[0],))

If anyone can figure this out I would be very grateful.

  • 1
    Figure *what* out? You've posted invalid Python code, and given no indication of what problem(s) you are having with it. – Scott Hunter Dec 01 '21 at 19:23
  • The solutions below are better. But to answer your original question. Have you looked at the value of `book`? Are you sure that `book[0]` is the title? Why are you even doing `SELECT *` instead of `SELECT title ...` – Frank Yellin Dec 01 '21 at 20:16

1 Answers1

1

Skip the loop and do

c.execute("DELETE FROM books;")
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30