Python 尝试使用用户输入删除时出现连接器错误

Python connector error on trying to delete using user input

我已经用类似的语法实现了大多数其他基本数据库事务,包括插入、更新、select,但是在尝试删除时,我得到错误

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

正确的语法是什么?我必须根据用户输入删除。这是我的代码的简化版本,减去插入,select,更新部分。:

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)

您需要将查询参数作为 元组 传递给 cursor.execute(),即使对于单个参数也是如此。试试这个:

sql = "DELETE FROM users WHERE email = %s"
cursor.execute(sql, (email,))