1

I am trying to store items, urls and prices within a MySQL database and wanted to give the user the option to delete the item based on the name. I am trying to do this by using a DELETE sql statement but it just returns "0 item removed" and I am guessing it is not able to find the entry, whether that be that it is not taking the input or what. Any help would be appreciated as I would really like to get the hang of this!

def deleteItem(): 
    goneItem = input("Enter item: ")
    removeItem = "DELETE FROM amazon_items WHERE itemName = '%s'"
    mycursor.execute(removeItem, goneItem)
    db.commit()
    print(mycursor.rowcount, "item removed")

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

2 Answers2

2

Try removing the single quote around %s and you should pass the value for %s as a tuple

def deleteItem(): 
    goneItem = input("Enter item: ")
    removeItem = "DELETE FROM amazon_items WHERE itemName = %s"
    mycursor.execute(removeItem, (goneItem,))
    db.commit()
    print(mycursor.rowcount, "item removed")
San
  • 453
  • 3
  • 14
0

Hi LearningCode welcome to stackoverflow,

mycursor.execute(removeItem, (goneItem,))

Try this one.

You can see in here in the documentation you need to include a comma to make the second argument a tuple.

Monata
  • 308
  • 1
  • 9
  • 1
    Hello. Thankyou for the help! After making the change you suggested I was still finding an error that there was an issue and would display 'itemname''' in the error. I guess part of the issue was the way i was using double quotes and that was causing a problem with it so had to change the query to 'query' instead of "query". – LearningCode May 27 '20 at 21:34
  • In the process of changing the Python string literal's quotes I assume you removed the quotes around `%s`? That was the issue. – Ilja Everilä Jun 03 '20 at 07:01