Search code examples
pythonmysql-python

MySQL-python Delete Query is NOT deleting data, but no error is displayed


I'm working on a terminal-password-manager with Python and SQL, everything works fine except for the delete function, I've managed to set it up in a way where no errors are displayed, however the delete query doesn't seem to be working because it doesn't actually delete the row from the table. So I can't really understand why this happening because no errors are thrown.

As you can see in this image: the problem

Here is the code:

delete entry function:

    def deleteEntry(sitename, siteurl, email, username):
        # check entry existence
        if checkEntryForDelete(sitename):
            printc("[yellow][-] There aren't any entries with those parameters...[/yellow]")
            return

        # remove from database  
        db = dbconfig()
        cursor = db.cursor()
        query = "DELETE FROM pmdatabase.entries WHERE sitename = '{sitename}' AND siteurl = '{siteurl}' AND email = '{email}' AND username = '{username}'"
        #val = (sitename,siteurl,email,username)
        cursor.execute(query)
        db.commit()

        printc("[red][-][/red] Entry Deleted ")

main file:

    if args.option in ["delete","d"]:
        if args.name == None or args.url == None or args.login == None:
            if args.name == None:
                printc("[red][!] Site Name (-s) is required and was not specified[/red]")
            if args.url == None:
                printc("[red][!] Site URL (-u) is required and was not specified[/red]")
            if args.login == None:
                printc("[red][!] Site Login User or Name (-l) is required and was not specified[/red]")
            return
        if args.email == None:
            printc("[red][!] Site Email (-l) is required and was not specified[/red]")
        
        res = inputAndValidateMasterPassword()
        if res is not None:
            utils.delete.deleteEntry(args.name,args.url,args.email,args.login)
            #utils.delete.deleteEntry(args.name)    

Any kind of help is appreciated.


Solution

  • Using the LIMIT 1 in my query (suggested by @wkl in the comments) made it so that the query worked in MySQL if I passed the values right into it, like:

    DELETE FROM pmdatabase.entries WHERE sitename = 'test' AND siteurl = 'test.com' AND email = '[email protected]' AND username = 'testuser' limit 1

    but this wouldn't work with my Python code because I need the values to be taken from the user input, that's why I decided to use placeholders (%s) to then pass the values into them with a tuple with parameter values:

    # remove from database  
        db = dbconfig()
        cursor = db.cursor()
        query = "DELETE FROM pmdatabase.entries WHERE sitename = %s AND siteurl = %s AND email = %s AND username = %s LIMIT 1"
        tuple1 = (sitename,siteurl,email,username)
        cursor.execute(query, tuple1)
        db.commit()
    

    And that seems to work.

    delete-works