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

Computer Project

Uploaded by

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

Computer Project

Uploaded by

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

import mysql.

connector
from datetime import datetime

# Establish the connection to the MySQL database


def connect_to_db():
return mysql.connector.connect(
host="localhost",
user="root",
password="yourpassword", # Change this to your password
database="employee_db"
)

# Create a table for employees


def create_table():
db = connect_to_db()
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS employees (
EmployeeId INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255),
Department VARCHAR(255),
Address VARCHAR(255),
PhoneNumber VARCHAR(15),
Salary DECIMAL(10, 2),
ApplicationDate DATE,
DateOfBirth DATE
)
""")
db.commit()
cursor.close()
db.close()

# Insert employee data


def insert_employee():
db = connect_to_db()
cursor = db.cursor()

name = input("Enter Name: ")


department = input("Enter Department: ")
address = input("Enter Address: ")
phone_number = input("Enter Phone Number: ")
salary = float(input("Enter Salary: "))
application_date = input("Enter Application Date (YYYY-MM-DD): ")
dob = input("Enter Date of Birth (YYYY-MM-DD): ")

query = """INSERT INTO employees (Name, Department, Address, PhoneNumber, Salary, ApplicationDate,
DateOfBirth)
VALUES (%s, %s, %s, %s, %s, %s, %s)"""
values = (name, department, address, phone_number, salary, application_date, dob)

cursor.execute(query, values)
db.commit()

print(f"Employee {name} added successfully.")

cursor.close()
db.close()

# Update employee data


def update_employee():
db = connect_to_db()
cursor = db.cursor()

employee_id = input("Enter EmployeeId to update: ")

column = input("Enter the column to update (Name, Department, Address, PhoneNumber, Salary,
ApplicationDate, DateOfBirth): ")
new_value = input(f"Enter the new value for {column}: ")

query = f"UPDATE employees SET {column} = %s WHERE EmployeeId = %s"


cursor.execute(query, (new_value, employee_id))

db.commit()

print(f"Employee {employee_id} updated successfully.")

cursor.close()
db.close()

# Delete an employee record


def delete_employee():
db = connect_to_db()
cursor = db.cursor()

employee_id = input("Enter EmployeeId to delete: ")

query = "DELETE FROM employees WHERE EmployeeId = %s"


cursor.execute(query, (employee_id,))

db.commit()

print(f"Employee {employee_id} deleted successfully.")

cursor.close()
db.close()

# Search for an employee


def search_employee():
db = connect_to_db()
cursor = db.cursor()

column = input("Enter the column to search by (EmployeeId, Name, Department, etc.): ")
value = input(f"Enter the value to search for in {column}: ")

query = f"SELECT * FROM employees WHERE {column} = %s"


cursor.execute(query, (value,))

results = cursor.fetchall()

for row in results:


print(row)

cursor.close()
db.close()

# Display the menu


def display_menu():
while True:
print("\nEmployee Database Management")
print("1. Insert a new employee")
print("2. Update an employee record")
print("3. Delete an employee")
print("4. Search for an employee")
print("5. Exit")

choice = input("Enter your choice: ")

if choice == '1':
insert_employee()
elif choice == '2':
update_employee()
elif choice == '3':
delete_employee()
elif choice == '4':
search_employee()
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")

# Main execution
if __name__ == "__main__":
create_table()
display_menu()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy