Tkinter登录页匹配SQL数据库记录时遇'Unread result found'错误求助
问题:Tkinter登录页连接MySQL时出现'Unread result found'错误
我正在做一个SQL-Python连接器的学习项目,做了一个简单的Tkinter登录页用来输入用户名和密码,同时搭建了存储账号密码的数据库。目标是通过SQL-Python连接器把登录页的输入和数据库记录匹配,但每次运行都会报'Unread result found'错误,不知道原因。附上相关代码,预期匹配成功时输出成功提示,求帮助。
import tkinter as tk import mysql.connector class LoginPage(tk.Tk): def __init__(self): super().__init__() self.title("Login Page") # Username Label and Entry username_label = tk.Label(self, text="Username:") username_label.pack() self.username_entry = tk.Entry(self) self.username_entry.pack() # Password Label and Entry password_label = tk.Label(self, text="Password:") password_label.pack() self.password_entry = tk.Entry(self, show="*") self.password_entry.pack() # Login Button login_button = tk.Button(self, text="Login", command=self.login_submit()) login_button.pack() def login_submit(self): # Database connection configuration config = { "user": "username", "host": "hostname", "password": "password", "database": "database_name", "raise_on_warnings": True } try: db = mysql.connector.connect(**config) print("Connection successful") except mysql.connector.Error as err: print("Something is wrong with your connection") print(err) return # Collecting data from entries username = self.username_entry.get() password = self.password_entry.get() # Creating a cursor cursor = db.cursor() # Query to select matching username and password from the main_db table sql = f"SELECT * FROM main_db WHERE username = '{username}' AND password = '{password}'" try: # Execute the SQL query cursor.execute(sql) # Fetch a single row result = cursor.fetchone() if result: print("Successful") else: print("Nope") except mysql.connector.Error as err: print("Error occurred during query execution") print(err) # Close the database connection db.close() if __name__ == "__main__": login_page = LoginPage() login_page.mainloop()
问题原因及解决方法
1. 按钮绑定错误
你在绑定登录按钮的command参数时,直接调用了self.login_submit(),这会导致程序初始化时就执行这个方法,而非点击按钮时触发。正确写法是传入方法名(不带括号):
login_button = tk.Button(self, text="Login", command=self.login_submit)
2. SQL注入风险与查询写法问题
用字符串格式化拼接SQL语句不仅存在严重的SQL注入漏洞,还可能引发'Unread result found'错误。必须改用参数化查询:
sql = "SELECT * FROM main_db WHERE username = %s AND password = %s" cursor.execute(sql, (username, password))
3. 未读取的结果集
当游标存在未读取的结果时,会触发该错误。可以在创建游标时指定buffered=True,确保结果被正确读取:
cursor = db.cursor(buffered=True)
4. 资源管理不规范
建议用with语句管理数据库连接和游标,确保资源自动释放,避免因连接未正确关闭引发的问题。
修正后的完整代码
import tkinter as tk import mysql.connector class LoginPage(tk.Tk): def __init__(self): super().__init__() self.title("登录页") # 用户名标签与输入框 username_label = tk.Label(self, text="用户名:") username_label.pack() self.username_entry = tk.Entry(self) self.username_entry.pack() # 密码标签与输入框 password_label = tk.Label(self, text="密码:") password_label.pack() self.password_entry = tk.Entry(self, show="*") self.password_entry.pack() # 登录按钮(修复command绑定) login_button = tk.Button(self, text="登录", command=self.login_submit) login_button.pack() def login_submit(self): # 数据库连接配置 config = { "user": "username", "host": "hostname", "password": "password", "database": "database_name", "raise_on_warnings": True } # 获取输入内容 username = self.username_entry.get() password = self.password_entry.get() try: # 使用with管理连接和游标,自动释放资源 with mysql.connector.connect(**config) as db: with db.cursor(buffered=True) as cursor: # 参数化查询 sql = "SELECT * FROM main_db WHERE username = %s AND password = %s" cursor.execute(sql, (username, password)) result = cursor.fetchone() if result: print("登录成功") else: print("用户名或密码错误") except mysql.connector.Error as err: print("数据库操作出错") print(err) if __name__ == "__main__": login_page = LoginPage() login_page.mainloop()
内容的提问来源于stack exchange,提问作者Zakii
相关产品推荐
相关产品推荐

