ValueError: Could not process parameters in Python/MySQL

ValueError: Could not process parameters in Python/MySQL

我是新手

我想防止人们每次注册时使用重复的用户名。

这是我截取的代码:

def submit(self):
    username_info = username.get()
    username_password = password.get()

    #connect to db
    db = mysql.connector.connect(host = 'localhost', user = 'root', password = '', database = 'user')

    #create a cursor
    mycursor = db.cursor()
    #insert to db
    sql = ("INSERT INTO useraccess (user_type, password) VALUES (%s, %s)")

    query = (username_info, username_password)
    mycursor.execute(sql, query)
    #commit
    db.commit()

    #create a messagebox
    messagebox.showinfo("Registration", "Successfully Register")

    #if username has been used
    find_user = ("SELECT * FROM useraccess WHERE user_type = ?")
    user_query = (username_info)

    mycursor.execute(find_user, user_query)
    #if (username == username_info):
    if mycursor.fetchall():
        messagebox.showerror("Registration", "The username chosen is already used. Please select another username")
    else:
        messagebox.showinfo("Registration", "Account Created!")

但是我每次运行它,虽然用户名已经在数据库中注册,但它只显示成功创建的消息框和错误:

ValueError: Could not process parameters.

谁能帮我解决这个问题?

我认为问题的根源在于线路

user_query = (username_info)

应该是

user_query = (username_info,)

尾随逗号是括号中的表达式与tuple.

之间的语法差异

代码的另一个问题是查询:

find_user = ("SELECT * FROM useraccess WHERE user_type = ?")

应该是:

find_user = ("SELECT * FROM useraccess WHERE user_type = %s")

你检查过这些变量了吗,

username_info = username.get()
username_password = password.get()

它们是可处理的格式吗? (也就是说,你能直接把 username.get() 放到 user_type 中吗?)

我不熟悉这种传递参数的方式

find_user = ("SELECT * FROM useraccess WHERE user_type = ?")

你仔细检查过了吗? (为什么不是 %s 方法?)

此外,您可能得到 "Account Created!" 因为 mycursor.fetchall() 失败。