I have created a program in Tkinter in which users first have to choose whether s/he wants to sign in or sign up, I have completed the sign-up part of my application but having trouble with the sign-in part.
I want to check if the user has already signed up or not when s/he will enter their details on the sign-up screen using python, Tkinter, and MySQL. I don't understand or have any idea how can I do that using MySQL-connector...

CodePudding user response:
Once you have a prepared-cursor object (I assume you have a cursor object, at least, since you have sign-up working?), you might issue a query such as this one:
cursor.execute(“SELECT COUNT(*) FROM users_table_name WHERE username=%s AND password_hash=%s”, (username_input, password_input)) # or whatever your authentication logic should be in the WHERE clause
result = cursor.fetchone()[0] # will be 1 if there is such a row, 0 if there is not
What this does:
cursor.fetchone()returns a tuple containing as many comma-separated columns/values as you specified betweenSELECTandFROM--which in this case is just one value: the "count" of rows that match the condition in theWHEREclause (i.e., having the right username and password)- Using a prepared cursor is critical, when user input will be a part of your query, in order to prevent SQL injection attacks. You want to avoid using any of the Python string-interpolation/string-building techniques to put the username or password the user entered into your query string, because this could allow the user to execute arbitrary SQL.
CodePudding user response:
if you want to build a simple query, check this w3school tutorial, you would just query the email to make sure the user is actually registered

