1

I have implemented most other basic database transactions including insert,update,select with similar syntax,but on trying to delete,i get error

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

What would the correct syntax be? I must delete according to user input. Here is a shortened version of my code,minus the insert,select,update part.:

elif (choice == 4):
    mail=raw_input('Enter email of user to be deleted:')
    print 'Deleting..'
    delete_user_details(mail)


def delete_user_details(email):

    sql = "DELETE FROM users WHERE email = %s"
    cursor.execute(sql,email)
2
  • Is that all of your code? The sql line isn't at line 1 Commented Jun 27, 2016 at 12:48
  • Knowing error is on the first line, maybe your connection string has some problems? You should post more. Commented Jun 27, 2016 at 12:50

1 Answer 1

4

You need to pass query parameters to cursor.execute() as a tuple, even for a single parameter. Try this:

sql = "DELETE FROM users WHERE email = %s"
cursor.execute(sql, (email,))
Sign up to request clarification or add additional context in comments.

2 Comments

Ah!! Thanks a lot!! I don't know how i missed that in the documentation.
Silly syntax error like missing the comma in the tuple was giving me a headache. Always a joy to see the right code and notice the mistake in mine! Thanks

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.