Skip to content
Advertisement

Login method using MySql.connector not working properly

I created a a method in pythin so that user could login as a user or as an admin but the program is not working as I expected it to be, I am posting the whole program, but mainly the error is coming in login method as I am not able to create a proper logic required for the method.

mycon = ms.connect(host = 'localhost', user = 'Nifty', passwd = 'root', database = 'Warranty')
mycursor = mycon.cursor()
u = ''
name = ''

def mainProgram(data,name):
    print("Hi", name)
    if data == 'user':
        print("What do you want to do?n1. Show your Datan2. Enter Data")
        c = int(input("Enter your choice: "))
        if c==1:
            data = int(input("Enter Automobile number: "))
            showData(data)
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()
        elif c==2:
            enterData()
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()
        else:
            print("Enter a valid choice.")
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()
    elif data == 'admin':
        print("What do you want to do?n1.Show datan2.Delete Recordn3.Enter Record")
        c = int(input("Enter you choice: "))
        if c == 1:
            showwholeData()
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()
        elif c == 2:
            data = int(input("Enter Automobile number: "))
            deleteData(data)
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()    
        elif c == 3:
            enterData()
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()
        else:
            print("Enter valid choice.")
            x = int(input("What do you want to do?n1. Continuen2. Logout"))
            if x==1:
                mainProgram(u, name)
            else:
                Intro()

def deleteData(data):
    query = "delete from Automobile where Automobile_Number = {0}".format(data)
    mycursor.execute(query)
    print("Data is deleted.")
    mainProgram(u, name)

def createACC():
    user_Name = input("Enter Username: ")
    pin = int(input("Enter pin: "))
    query = "insert into accountList values('{0}', {1})".format(user_Name, pin)
    mycursor.execute(query)
    mycon.commit()
    print("We have registered your account succcessfully.")
    input("Press anything to continue.")
    Intro()

def login():
    x = []
    print("Enter data to login: ")
    name = input("Enter Username: ")
    pin = int(input("Enter pin: "))
    x.append([name, pin])
    query = "select * from accountList where user_Name = '{0}' and Pin = {1}".format(name, pin)
    mycursor.execute(query)
    results= mycursor.fetchall()
    query1 = "select * from adminList where user_Name = '{0}' and Pin = {1}".format(name, pin)
    mycursor.execute(query1)
    aresults= mycursor.fetchall()
    for ch in x:
        if ch in results:
            print("Login successfull")
            u = 'user'
            mainProgram(u,name)
            return(u)
        elif ch in aresults:
            print("admin login successfull")
            u = 'admin'
            mainProgram(u,name)
            return(u)
    else:
        print("Enter valid details")
        c = input("Try Again?[y/n]: ")
        if c.lower() == "y":
            login()
        else:
            Intro()
    return(name)

def showData(data):
    query = "select * from Automobile where Automobile_Number = {0}".format(data)
    mycursor.execute(query)
    results= mycursor.fetchall()
    print("Here is your Warranty List: ")
    print(results)
    input("Press anything to continue.")
    mainProgram(u,name)

def enterData():
    ANum = int(input("Enter Automobile Number: "))
    AName = input("Enter Automobile Name: ")
    Abr = input("Enter Automobile Brand: ")
    query = "insert into Automobile values({0},'{1}','{2}')".format(ANum, AName, Abr)
    mycursor.execute(query)
    mycon.commit()
    print("We have registered your vehicle succcessfully.")
    input("Press anything to continue.")
    mainProgram(u, name)

def Intro():
    print("Welcome to Automobile Warranty")
    print("What do you want to do?n1. Create Accountn2. Login")
    c = int(input("Enter your choice: "))
    if c==1:
        createACC()
    elif c==2:
        login()

def showwholeData():
    query = "select * from Automobile"
    mycursor.execute(query)
    results= mycursor.fetchall()
    print("Here is your Warranty List: ")
    print(results)
    input("Press anything to continue.")
    mainProgram(u, name)




Intro()

I expected the login to work properly but so that user can have two different methods of login which gave two different interactable options but it is bypassing the for loop and directly going to else loop in the login method, rest of the code is working fine if we bypas the login method altogether but the method is neccessary for the project but I am not able to come up with the solution for creating the proper mehtod.

Advertisement

Answer

You’re making this much more complicated than necessary. The query already checks whether the values match, you don’t need your own loop after the query to test that again. Just check whether each query returned any results.

You also shouldn’t use recursion as a substitute for looping. Use a while True: loop and break out of it when the user says they don’t want to try again.

def login():
    while True:
        print("Enter data to login: ")
        name = input("Enter Username: ")
        pin = int(input("Enter pin: "))

        query = "select 1 from accountList where user_Name = %s and Pin = %s"
        mycursor.execute(query, (name, pin))
        results= mycursor.fetchone()
        if results:
            u = 'user'
            mainProgram(u, name)
            return u

        query1 = "select 1 from adminList where user_Name = %s and Pin = %s"
        mycursor.execute(query1, (name, pin))
        aresults= mycursor.fetchone()
        if aresults:
            u = 'admin'
            mainProgram(u, name)
            return u

        print("Enter valid details")
        c = input("Try Again?[y/n]: ")
        if c.lower() != "y":
            break

    return(name)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement