ValueError: Could not process parameters in Python/MySQL

假装没事ソ 提交于 2020-03-03 04:21:18

问题


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:

ValueError: Could not process parameters.

Anyone can help me to solve this problem?


回答1:


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



回答2:


Have you checked these variables,

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

are they in proccesable formats? (i.e. can you directly put the username.get() into 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?)

also, you probably get the "Account Created!" because mycursor.fetchall() fails.



来源:https://stackoverflow.com/questions/55178173/valueerror-could-not-process-parameters-in-python-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!