0

I have been reading a number of threads on how to accomplish this but for some reason it is not working.

I need to delete a Row from a database using a string variable from an entry widget as the "WHERE (variable name)=" is used in the DB query. The entry widget data is stored as Snippet_Name and the same name is being used as a column name in the DB.

The database has 7 columns but I am only using the 1 column for the query and I want to delete the complete row which contains the entry variable. I have tried variations of DELETE with no success.

The code being used is:

def delete_code():
    try:
        snippetname = Snippet_Name.get()
        sql_delete_query = ('DELETE FROM Code WHERE Snippet_Name = "?"', (snippetname))
        c.execute(sql_delete_query)
        conn.commit()

    except:
        messagebox.showerror('PYSnippet', 'Failed to delete record')

A little help and hint would be appreciated.

DT2000
  • 63
  • 2
  • 9
  • This should help https://stackoverflow.com/a/517471/7870866 – alter123 Apr 30 '20 at 07:26
  • I have looked at the replies in the thread that you provided and I can't see how this will help resolve my problem. The format that I have in my example above should be able to use the variable "snippetname", which should then contain the entry data from the widget "Snippet_Name". That being said, shouldn't it be able to be used to delete the Row in the database that contains that word found in the database column named Snippet_Name or do I need to find the RowID in order to delete the whole Row? – DT2000 May 01 '20 at 00:08

1 Answers1

0

I went over the query and found 2 errors that needed to be address to correct the problem. The first problem was I had the ? enclosed in quotations which should not be there. Second problem was forgetting to a comma to the variable.

def delete_code():
    try:
        snippetname = Snippet_Name.get()
        sql_delete_query = ('DELETE FROM Code WHERE Snippet_Name = ?' (snippetname,))
        c.execute(sql_delete_query)
        conn.commit()

    except:
        messagebox.showerror('PYSnippet', 'Failed to delete record')
DT2000
  • 63
  • 2
  • 9