Is there a way I can match the records from sql database to tkinter entry text?

53 views Asked by At

"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

0

There are 0 answers