尝试使用用户输入删除时出现 Python 连接器错误 [英] Python connector error on trying to delete using user input

查看:75
本文介绍了尝试使用用户输入删除时出现 Python 连接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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): 你的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在第 1 行的%s"附近使用的正确语法

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)

推荐答案

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

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,))

这篇关于尝试使用用户输入删除时出现 Python 连接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆