I'm trying to make a simple tkinter program where there's entries to put your username, email, and password and I am trying to make it check if there a username already exists and it gives me this error. I know there has been other questions about it on stackoverflow but those did not help me out.
import mysql.connector
from tkinter import *
root = Tk()
root.title("Login")
root.iconbitmap("profile.ico")
db = mysql.connector.connect(
db = "pythonlogin",
host="127.0.0.1",
user="root",
password="[password]"
)
cursor = db.cursor()
usernameLabel = Label(root, text='Username: ')
emailLabel = Label(root, text='Email: ')
passwordLabel = Label(root, text='Password: ')
usernameEntry = Entry(root, width=30)
emailEntry = Entry(root, width=30)
passwordEntry = Entry(root, width=30)
usernameLabel.grid(row=0, column=0)
emailLabel.grid(row=1, column=0)
passwordLabel.grid(row=2, column=0)
usernameLabel.config(font=("Roboto", 12))
emailLabel.config(font=("Roboto", 12))
passwordLabel.config(font=("Roboto", 12))
usernameEntry.grid(row=0, column=1)
emailEntry.grid(row=1, column=1)
passwordEntry.grid(row=2, column=1)
class userInfo():
def __init__(self, username, email, password):
self.username = username
self.email = email
self.password = password
def addInfoToDB(self):
query = "INSERT INTO pylogin (Username, Email, Password) VALUES (%s, %s, %s)"
values = (self.username, self.email, self.password)
cursor.execute(query, values)
db.commit()
print(cursor.rowcount, "record inserted.")
errorLabel = Label(root)
errorLabel.grid(row=4, column=1)
def submitInfo():
fetchInfo = cursor.fetchall()
if usernameEntry.get() == "" or emailEntry.get() == "" or passwordEntry.get() == "":
errorLabel.config(text="Error: Couldn't get value of all entries", fg="red")
if (usernameEntry.get(),) in fetchInfo:
errorLabel.config(text="Username already exists in the database!")
else:
errorLabel.config(text="Success!", fg="green")
details = userInfo(usernameEntry.get(), emailEntry.get(), passwordEntry.get())
details.addInfoToDB()
submitButton = Button(root, text="Submit", command=submitInfo)
submitButton.grid(row=3, column=1, ipadx=50)
submitButton.config(font=("Roboto", 10))
root.mainloop()