我一直对这段代码有疑问。我正在尝试制作一个带有 2 个输入框、一个手机框和一个密码框的登录页面(手机是 MySQL 表中的主键)。
import tkinter as tk
import re
import mysql.connector as ms
mycon = ms.connect (host = 'localhost', user='root', password='Welcome@123', database='cs_project')
cursor = mycon.cursor()
root = tk.Tk()
root.geometry('2560x1600')
root.title("Hotel")
label = tk.Label(root, text='Welcome!', font=('Baskerville', 60), fg='Purple')
label.pack(padx=20, pady=20)
def Login():
global LI_Mobile
global LI_Password
def validate_numbers(action, value_if_allowed):
pattern = r'[0-9]*$'
return re.match(pattern, value_if_allowed) is not None
validate_numbers_cmd = root.register(validate_numbers)
LI_Mobile_Label = tk.Label(root, text='Mobile:', font=('Baskerville', 25))
LI_Mobile_Label.pack(padx=5, pady=5)
LI_Mobile = tk.Entry(root, font=('Big Caslon', 20), validate='key', validatecommand=(validate_numbers_cmd, '%d', '%P'))
LI_Mobile.pack(padx=5, pady=5)
LI_Password_Label = tk.Label(root, text='Password:', font=('Baskerville', 25))
LI_Password_Label.pack(padx=5, pady=5)
LI_Password = tk.Entry(root, font=('Big Caslon', 20))
LI_Password.pack(padx=5, pady=5)
AccountDoesntExist_Label = tk.Label(root, text='You do not have an account, please create one by signing up.', font=('Baskerville', 25))
AccountExists_Label = tk.Label(root, text='Welcome, please click the button below to be redirected to the Main Menu.', font=('Baskerville', 25))
WrongPassword_Label = tk.Label(root, text='Incorrect Password, please try again.', font=('Baskerville', 25))
def Submit2():
mobile2=LI_Mobile.get()
password2=LI_Password.get()
mobile3 = list(mobile2)
password3 = list(password2)
MobileExist = cursor.execute('SELECT * FROM Accounts WHERE mobile="%s")',(mobile3));
PasswordCorrect = cursor.execute('SELECT * FROM Accounts WHERE password="%s")',(password3));
if MobileExist is None and PasswordCorrect is None:
AccountDoesntExist_Label.pack(padx=5,pady=10)
elif MobileExist is None and PasswordCorrect is not None:
AccountDoesntExist_Label.pack(padx=5, pady=10)
elif PasswordCorrect is None and MobileExist is not None:
WrongPassword_Label.pack(padx=5, pady=10)
else:
AccountExists_Label.pack(padx=5, pady=5)
MenuButton.pack(padx=5,pady=10)
Submit2_Button = tk.Button(root, text='Submit', font=('Baskerville', 25), command=Submit2)
Submit2_Button.pack (padx=5, pady=5)
LoginButton = tk.Button(root, text= "Log in", font=('Baskerville', 30), command=Login)
LoginButton.pack(padx=10, pady=10)
MenuButton = tk.Button(root, text= "Menu", font=('Baskerville', 30))
root.mainloop()
起初,mobile2 是一个整数值,password2 是字符串,但随后我收到了此错误:
“mysql.connector.errors.ProgrammingError:无法处理参数:int(1234567890),它必须是列表、元组或字典类型”
(我在Mobile的输入框中输入了值1234567890)
所以我强行将我的值更改为列表并再次尝试该程序,但我收到了一个新错误:-
mysql.connector.errors.ProgrammingError:SQL语句中未使用所有参数
我这部分程序的主要目标是接收用户输入,检查给定的值是否与 MySQL 表“Accounts”中的值匹配,然后通过菜单按钮(其命令我稍后会定义)。
我认为需要改变的主要内容是以下几行:
MobileExist = cursor.execute('SELECT * FROM Accounts WHERE mobile="%s")',(mobile3));
PasswordCorrect = cursor.execute('SELECT * FROM Accounts WHERE password="%s")',(password3));