0% found this document useful (0 votes)
8 views

Password Management

Uploaded by

Dinesh Karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Password Management

Uploaded by

Dinesh Karan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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()

You might also like