Python Forum
Python project help (Password manger using mysql)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python project help (Password manger using mysql)
#1
I am making a python project using mysql a simple password manager it has 2 files one is the one that creates a database named as setup.py and the other one is the main program i want to know how can using classes in this program i am not getting a way to implement the concept of class in it plzzz help!!!!

import mysql.connector

db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    database="passwd",
    auth_plugin='mysql_native_password'
)

print("PLEASE MAKE SURE THAT YOU HAVE RUN \"setup.py\" BEFORE!!!!!")
print(db)
print("Successfully connected to the database")


mycursor = db.cursor()


def mainmenu():
    print(" ")

    print("WELCOME TO PASSWORD MANAGER")
    print("MAIN MENU:")

    print(" ")

    print("Enter \"See all\" to show all the passwords.")
    print("Enter \"New\" to enter a new password.")
    print("Enter \"Delete\" to delete a password.")
    print("Enter \"Search\" to search for any password.")
    print("Enter \"Reset\" to reset the whole database and to remove all the passwords.")

    print(" ")

    user_input = input("Please enter option from the above given menu: ")

    if user_input == "See all":
        see_allpasswds()
    elif user_input == "New":
        add_newpasswd()
    elif user_input == "Delete":
        delete_passwd()
    elif user_input == "Search":
        search()
    elif user_input == "Reset":
        reset_database()
    else:
        print("ERROR: The option you have chosen is incorrect")
        print("Please choose the option from the given menu!!!")
        print(" ")

        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def see_allpasswds():
    print(" ")

    mycursor.execute("SELECT * FROM passwords")
    results = mycursor.fetchall()
    for x in results:
        print(x)
    print("All the data in the database.")
    print(" ")

    print("Enter \"Back\" to go back to the menu.")
    userInput = input("Enter option: ")
    if userInput == "Back":
        menu()
    else:
        print("ERROR:Wrong input please rerun the program.")


def add_newpasswd():
    print("  ")

    print("Add data accordingly as given.")
    websites = input("Enter Website: ")
    usernames = input("Enter Username or an Email_Id: ")
    passwords = input("Enter the password: ")
    dates = input("Enter the date when password created/uploaded in yyyy/mm/dd format: ")

    sql = "INSERT INTO passwords (Website, Username, Password, Date) VALUES (%s, %s, %s, %s)"
    value = (websites, usernames, passwords, dates)

    mycursor.execute(sql, value)
    db.commit()

    print("Your password has been inserted into the database!!!")

    print(" ")

    print("Do you want to enter more passwords? ")
    print("Enter \"YES\" if you want to add.")
    print("Enter \"NO\" to go back to mainmenu.")
    userInput = input("Enter option: ")
    if userInput == "YES":
        add_newpasswd()
    elif userInput == "NO":
        menu()
    else:
        print("ERROR:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def delete_passwd():
    print("So you want to delete a password?")

    userInput = input("Enter \"YES\" OR \"NO\": ")

    if userInput == "YES":
        delete = input("Enter the website you want to delete the password for: ")
        sql = "DELETE FROM passwords WHERE website = %s"
        val = (delete,)
        mycursor.execute(sql, val)
        db.commit()
        print("The data saved in the website you choose has been deleted from database.")
        print(" ")
        print("Do you want to delete again? ")
        print("Enter \"YES\" to delete again.")
        print("Enter \"NO\" to go back to the mainmenu")
        user_input = input("Enter option: ")
        if user_input == "YES":
            delete_passwd()
        elif user_input == "NO":
            menu()
        else:
            print("ERROR:Wrong Input")
            print("Enter \"Back\" to go back to the menu.")
            userInput = input("Enter option: ")
            if userInput == "Back":
                menu()
            else:
                print("ERROR:Wrong input please rerun the program")

    elif userInput == "NO":
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")

    else:
        print("Error:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def search():
    print(" ")
    print("You can now choose how you want to search the database")
    print("Enter \"Website\" if you want to search for a website")
    print("Enter \"Username\" if you want to search for a username")
    print("Enter \"Password\" if you want to search for a password")

    searchInput = input("Enter your option: ")

    if searchInput == "Website":
        website_search()
    elif searchInput == "Username":
        username_search()
    elif searchInput == "Password":
        passwd_search()
    else:
        print("ERROR:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def website_search():
    print("You have choose to search the database using website name.")

    searchWebsite = input("Enter website: ")

    sql = "SELECT * FROM passwords WHERE Website = %s"
    val = (searchWebsite,)
    mycursor.execute(sql, val)
    sresult = mycursor.fetchall()
    for x in sresult:
        print(x)
    print(" ")

    print("Do you want to search database again.")
    print("Enter \"YES\" to search again.")
    print("Enter \"NO\" to go back to the menu")
    userInput = input("Enter option: ")
    if userInput == "YES":
        search()
    elif userInput == "NO":
        menu()
    else:
        print("ERROR:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def username_search():
    print("You have choose to search the database using username.")

    searchUsername = input("Enter Username: ")

    sql = "SELECT * FROM passwords WHERE Username = %s"
    val = (searchUsername,)
    mycursor.execute(sql, val)
    sresult = mycursor.fetchall()
    for x in sresult:
        print(x)
    print(" ")

    print("Do you want to search database again.")
    print("Enter \"YES\" to search again.")
    print("Enter \"NO\" to go back to the menu")
    userInput = input("Enter option: ")
    if userInput == "YES":
        search()
    elif userInput == "NO":
        menu()
    else:
        print("ERROR:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def passwd_search():
    print("You have choose to search the database using password.")

    searchPasswd = input("Enter Password: ")

    sql = "SELECT * FROM passwords WHERE Password = %s"
    val = (searchPasswd,)
    mycursor.execute(sql, val)
    sresult = mycursor.fetchall()
    for x in sresult:
        print(x)
    print(" ")

    print("Do you want to search database again.")
    print("Enter \"YES\" to search again.")
    print("Enter \"NO\" to go back to the menu")
    userInput = input("Enter option: ")
    if userInput == "YES":
        search()
    elif userInput == "NO":
        menu()
    else:
        print("ERROR:Wrong Input")
        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


def reset_database():
    print("Enter \"RESET\" if you want to reset database.")
    print("Enter \"Back\" if you want to go back to menu.")

    reset = input("Enter Option: ")
    if reset == "RESET":
        database_reset()
    elif reset == "BACK":
        menu()
    else:
        print("ERROR:Wrong Input")


def database_reset():
    mycursor.execute("DELETE * FROM passwords")
    db.commit()

    print("Your database has been completely erased.")

    print(" ")

    print("Enter \"Back\" to go back to the menu.")
    userInput = input("Enter option: ")
    if userInput == "Back":
        menu()
    else:
        print("ERROR:Wrong input please rerun the program")


def app_exit():
    print("Thank You for using password manager!")
    print("BYE SEE YOU SOON")


def menu():
    print(" ")
    print("MAIN MENU:")
    print("Enter \"See all\" to show all the passwords.")
    print("Enter \"New\" to enter a new password.")
    print("Enter \"Delete\" to delete a password.")
    print("Enter \"Search\" to search for any password.")
    print("Enter \"Reset\" to reset the whole database and to remove all the passwords.")
    print("Enter \"Exit\" to exit the application.")
    print(" ")

    user_input = input("Please enter option from the above given menu: ")

    if user_input == "See all":
        see_allpasswds()
    elif user_input == "New":
        add_newpasswd()
    elif user_input == "Delete":
        delete_passwd()
    elif user_input == "Search":
        search()
    elif user_input == "Reset":
        reset_database()
    elif user_input == "Exit":
        app_exit()
    else:
        print("ERROR: The option you have chosen is incorrect")
        print("Please choose the option from the given menu!!!")
        print(" ")

        print("Enter \"Back\" to go back to the menu.")
        userInput = input("Enter option: ")
        if userInput == "Back":
            menu()
        else:
            print("ERROR:Wrong input please rerun the program")


mainmenu()
This one is the main program and the next one is the setup.py as i said above it makes the database

import mysql.connector

db = mysql.connector.connect(
    host="localhost",
    user="root",
    passwd="root",
    database="passwd",
    auth_plugin='mysql_native_password'
)

print(db)

mycursor = db.cursor()

mycursor.execute("CREATE TABLE passwords(Website varchar(20), Username varchar(30), "
                 "Password varchar(30), Date varchar(10) not null)")

print("You have successfully created your database.")
print("You can now use the password manager.")
print("THANK YOU!")
CAN SOMEONE PLZZZ HELP ME WITH THE IMPLEMENTATION OF OOP AND CLASSES IN MY PROGRAM
I DON'T UNDERSTAND HOW TO USE IT
Reply
#2
Two directions I can see for you to go. One is to figure out what candidates there are for classes. Making a "password" class seems overkill for that piece of data. You could make a menu class that handles the menu display and all of the activities. Or, more ambitiously, you could make a context manager that allows you to use "with" on your database. That requires you to implement __enter__ and __exit__ functions that would handle all of the opening and closing of the database connection. What are you looking for?
Reply
#3
OOP has become a catchphrase that many equate with "structured programming". If you code is organized well and easy to use it must be OOP, right? This is completely wrong of course.
The attitude that OOP is good and all else is bad has lead to a backlash against OOP and interesting articles asking if OOP is a good choice for any problem space.

I don't seeing a password database as a good fit for OOP. The only "object" is the database, and that object is modeled by the database API. I suppose if you really dislike the mysql API you could make an object oriented wrapper.

Is this an assignment where you have to use classes to solve the problem?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Password Saver Assignment sshellzr21 2 6,104 May-02-2020, 01:34 AM
Last Post: sshellzr21
  Password Saver Project jhenry 15 14,604 Oct-13-2017, 08:30 AM
Last Post: gruntfutuk
  [URGENT] Trying to create a simple password manager in python equanox314 5 11,118 Jul-28-2017, 08:04 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020