python typeerror valueerror,ValueError:无法在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:

ValueError: Could not process parameters.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值