0

"Recently, I was working on a project to learn more about the SQL-Python connector. In this project, I created a simple Tkinter login page to enter usernames and passwords. Additionally, I set up a database containing various usernames and passwords. My objective is to use the SQL-Python connector to match the records in the SQL database with the usernames and passwords entered on the Tkinter login page. However, every time I run the code, I encounter an 'Unread result found' error. I am unsure why this error is occurring. Can anyone help me with this?"

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

I was excepting if the username is matched in database it will tell a successful msg

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Zakii
  • 1
  • 1
  • Do you have multiple rows with the same username and password? You're only calling `fetchone`. Exactly where do you see this error? – Tim Roberts Jun 22 '23 at 04:38
  • No, In my db all username and passwords are unique. I see error in fetching the row – Zakii Jun 22 '23 at 04:55
  • Things to try: `LIMIT 1` in the SQL, `fetchall` instead of `fetchone`, set `buffered=True` when you create your cursor. – Tim Roberts Jun 22 '23 at 04:59
  • Yes, it worked. Thank you for your help and time you considered for this. – Zakii Jun 22 '23 at 05:05
  • Can you please tell me why does that happen? – Zakii Jun 22 '23 at 05:16
  • The evidence strongly suggests you have duplicate rows. After the `fetchone`, there were still more records waiting to be fetched. – Tim Roberts Jun 22 '23 at 06:45
  • The error means that you try to execute another SQL when there are still records not fetched. But your code exeutes only one SQL and then close the connection, so I wonder why you get such exception. – acw1668 Jun 23 '23 at 02:10
  • The code works fine after changing `command=self.login_submit()` to `command=self.login_submit` (without `()`). – acw1668 Jun 24 '23 at 22:01

0 Answers0