import mysql.
connector
from mysql.connector import Error
import getpass
# Function to connect to MySQL database
def create_connection():
connection = None
try:
connection = mysql.connector.connect(
host='localhost',
user='your_username', # Replace with your MySQL username
password='your_password', # Replace with your MySQL password
database='password_manager'
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return connection
# Function to create a new user
def create_user(connection, username, password):
cursor = connection.cursor()
cursor.execute("INSERT INTO users (username, password) VALUES (%s, %s)", (username, password))
connection.commit()
print(f"User {username} created successfully!")
# Function to retrieve a user's password
def retrieve_password(connection, username):
cursor = connection.cursor()
cursor.execute("SELECT password FROM users WHERE username = %s", (username,))
result = cursor.fetchone()
if result:
print(f"Password for {username}: {result[0]}")
else:
print("User not found!")
def main():
connection = create_connection()
while True:
print("\nPassword Manager")
print("1. Create User")
print("2. Retrieve Password")
print("3. Exit")
choice = input("Choose an option: ")
if choice == '1':
username = input("Enter username: ")
password = getpass.getpass("Enter password: ")
create_user(connection, username, password)
elif choice == '2':
username = input("Enter username: ")
retrieve_password(connection, username)
elif choice == '3':
break
else:
print("Invalid choice. Please try again.")
if connection:
connection.close()
if __name__ == "__main__":
main()