I'm using PyCharm to run Python (with tkinter
and mysql.connector
) and I am trying to run the following code:
from tkinter import *
import tkinter.messagebox as MessageBox
import mysql.connector as mysql
root = Tk()
top = Toplevel()
top.geometry("900x600")
top.title("LIMS")
global mycursor
db = mysql.connect(
host="localhost",
user="root",
password="mypass",
database="db name"
)
global user
def loginprogram():
user = e_username.get()
pw = e_password.get()
mycursor = db.cursor()
myuser_queue=[]
user_query = "SELECT username FROM user WHERE username =%s" % (username)
try:
mycursor.execute("SELECT username FROM user")
users = mycursor.fetchall()
for x in users:
print(x)
if (user == x):
print("Correct!")
myuser_queue.append(x)
print(myuser_queue)
except:
print('Error occurred')
if (user) in myuser_queue:
print("True " + user)
else:
print("Still wrong")
mycursor.execute("SELECT password FROM user")
passwords = mycursor.fetchall()
for x in passwords:
print(x)
def exitprogram():
top.destroy()
root.destroy()
sys.exit()
title = Label(top, text='Enter Log In Credentials', font={'Helvetica', 10})
title.place(x=20,y=30)
username = Label(top,text='Enter Username',font=('bold',10))
username.place(x=20,y=60)
e_username = Entry(top)
e_username.place(x=150, y=60)
password = Label(top,text='Enter Password',font=('bold',10))
password.place(x=20,y=90)
e_password = Entry(top)
e_password.place(x=150, y=90)
login_button = Button(top, text="Login", font=("bold", 10), bg="white",command=lambda: loginprogram())
login_button.place(x=150, y=120)
exitbutton = Button(top, text="Exit", font=("bold", 10), bg="white",command=lambda: exitprogram())
exitbutton.place(x=150, y=150)
root.withdraw()
root.mainloop()
I have it set up to print and have it correctly pulling in the username from the database. It is also correctly placing each username into the list. The issue appears to be in the section:
I have also tried several iterations of the query, but the one in the actual mycursor
appears to be the only one correctly pulling the information from the MySQL database.
if (user) in myuser_queue:
print("True " + user)