简体   繁体   English

ValueError:无法处理Python / MySQL中的参数

[英]ValueError: Could not process parameters in Python/MySQL

i am a newbie 我是新手

I want to prevent the duplicate usernames every time people do registration. 我想在每次人们注册时阻止重复的用户名。

Here is my snipped code: 这是我的剪辑代码:

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!")

But every time I run it, although the username has been registered in the db, it only shows the successfully created messagebox and error: 但是每次运行它时,虽然用户名已经在db中注册,但它只显示成功创建的消息框和错误:

ValueError: Could not process parameters. ValueError:无法处理参数。

Anyone can help me to solve this problem? 有人可以帮我解决这个问题吗?

I believe the source of the problem is in the line 我相信问题的根源就在于此

user_query = (username_info)

It should be 它应该是

user_query = (username_info,)

The trailing comma is the syntactic difference between an expression in parentheses and a tuple . 尾随逗号是括号中的表达式和元组之间的语法差异。

Another issue with code is the query: 代码的另一个问题是查询:

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

Which should be: 应该是:

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

Have you checked these variables, 你检查过这些变量吗

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

are they in proccesable formats? 他们是否采用可执行的格式? (ie can you directly put the username.get() into user_type ?) (即你可以直接将username.get()放入user_type吗?)

I'm not familiar with this way of passing a parameter 我不熟悉这种传递参数的方式

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

have you double checked this? 你仔细检查过这个吗? (why not the %s method?) (为什么不是%s方法?)

also, you probably get the "Account Created!" 另外,你可能会得到“帐户创建!” because mycursor.fetchall() fails. 因为mycursor.fetchall()失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM