ValueError:无法在Python/MySQL中处理参数 [英] ValueError: Could not process parameters in Python/MySQL
本文介绍了ValueError:无法在Python/MySQL中处理参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新手
我想防止每次注册时重复的用户名.
I want to prevent the duplicate usernames every time people do registration.
这是我的摘录代码:
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:无法处理参数.
ValueError: Could not process parameters.
任何人都可以帮助我解决这个问题吗?
Anyone can help me to solve this problem?
推荐答案
我相信问题的根源所在
user_query = (username_info)
应该是
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 = ?")
应为:
find_user = ("SELECT * FROM useraccess WHERE user_type = %s")
这篇关于ValueError:无法在Python/MySQL中处理参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文