Home > database >  Incorrect password message box
Incorrect password message box

Time:01-25

import mysql.connector
from tkinter import messagebox

from mysql.connector import Error, InternalError

logman = mysql.connector.connect(host = 'localhost',user = 'root', passwd = '12345678', database = 'basictest')

logwife = logman.cursor(buffered=True)

def wxy(use,pas):
    logboy = ('SELECT Username, Password FROM patient WHERE Password ='   str(pas))

    logwife.execute(logboy)

    x = list(logwife.fetchone())

    print(x)

    if x[0] == use:
        messagebox.showinfo('success','You have successfully Logged in')

    else:
        messagebox.showerror('Failed','Wrong Credentials')

I want to make it so that I shows a message box on incorrect password. But I throws an error

CodePudding user response:

The final SQL statement is invalid, for example if pas has value "pass", then the final SQL statement will be:

SELECT Username, Password FROM patient WHERE Password = pass

It is better to use placeholder.

Also you need to check whether record is returned, otherwise x = list(logwife.fetchone()) will raise exception.

Below is the updated wxy():

def wxy(use, pas):
    # use placeholder
    logboy = 'SELECT Username, Password FROM patient WHERE Password = %s'
    logwife.execute(logboy, (str(pas),)) # 2nd argument must be tuple or list
    x = logwife.fetchone()
    print(x)

    # better check whether x is None or not before using it
    if x is not None and x[0] == use:
        messagebox.showinfo('success', 'You have successfully Logged in')
    else:
        messagebox.showerror('Failed', 'Wrong Credentials')

I would suggest to check both username and password in the SQL:

def wxy(use, pas):
    logboy = 'SELECT 1 FROM patient WHERE Username = %s and Password = %s'
    logwife.execute(logboy, (use, pas))
    x = logwife.fetchone()
    print(x)
    if x is not None:
        messagebox.showinfo('success','You have successfully Logged in')
    else:
        messagebox.showerror('Failed','Wrong Credentials')

Note that it is not recommended to store plain text password in database for security issue.

CodePudding user response:

these lines of code give the error as you wanted.

Maybe there is something wrong with the else

from tkinter import messagebox

messagebox.showerror("Title", "Message")

enter image description here

  •  Tags:  
  • Related