Employee Management System Using Python
Employee Management System Using Python
Getting Started
For creating the Employee Management System in Python that uses MySQL database we
need to connect Python with MySQL.
For making a connection we need to install mysqlconnector which can be done by
writing the following command in the command prompt on Windows.
pip install MySQL connector
Now after successful installation of mysqlconnector we can connect MySQL with Python
which can be done by writing the following code.
Python1
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root",
password="password", database="emp")
Now we are Done with the connections, so we can focus on our Employee Management
System
Table in Use:
Employee Record
The idea is that we keep all the information about the Employee in the above table and
manipulate the table whenever required. So now we will look at the working of each
operation in detail.
The check employee function takes employee id as a parameter and checks whether any
employee with given id exists in the employee details record or not. For checking this it
uses cursor.rowcount() function which counts the number of rows that match with given
details. It is a utility function, and we will see its use in later operations like Add
employee function, etc.
Program:
Python2
# Function To Check if Employee with
# given Id Exist or Not
def check_employee(employee_id):
if r == 1:
return True
else:
return False
The Add Employee function will ask for the Employee Id and uses the check employee
function to check whether the employee to be added already exist in our record or not if
employee details do not already exist then it asks for details of the employee to be added
like Employee Name, Post of Employee and Salary of the employee. Now after getting all
such details from the user of that system it simply inserts the information in our
Employee details table.
Program:
Python3
# Function to mAdd_Employee
def Add_Employ():
else:
Name = input("Enter Employee Name : ")
Post = input("Enter Employee Post : ")
Salary = input("Enter Employee Salary : ")
data = (Id, Name, Post, Salary)
The Remove Employee Function will simply ask for Id of the employee to be removed
because Id is Primary key in our Employee Details Record as there can be two employees
with the same name, but they must have a unique id. The Remove Employee function
uses the check employee function to check whether the employee to be removed exists in
our record or not if employee details exist then after getting a valid employee id it deletes
the record corresponding to that employee id.
Program:
Python4
# Function to Remove Employee with given Id
def Remove_Employ():
Id = input("Enter Employee Id : ")
else:
The Promote Employee function will ask for the Employee Id and uses the check
employee function to check whether the employee to be Promoted exist in our record or
not if employee details exist then it will ask for the amount by which his salary is to be
increased. After getting the valid details it increases the salary of the employee with the
given id by the given amount.
Program:
Python5
# Function to Promote Employee
def Promote_Employee():
Id = int(input("Enter Employ's Id"))
The Display Employees function is simply a select query of SQL which fetches all the
records stored in the employee details table and prints them line by line.
Program:
def Display_Employees():
Menu Function
The Menu function displays the menu to the user and asks the user to enter his choice for
performing operations like Add employee, Remove employee, etc.
Program:
def menu():
print("Welcome to Employee Management Record")
print("Press ")
print("1 to Add Employee")
print("2 to Remove Employee ")
print("3 to Promote Employee")
print("4 to Display Employees")
print("5 to Exit")
elif ch == 2:
Remove_Employ()
elif ch == 3:
Promote_Employee()
elif ch == 4:
Display_Employees()
elif ch == 5:
exit(0)
else:
print("Invalid Choice")
menu()
Complete Code:
# making Connection
con = mysql.connector.connect(
host="localhost", user="root", password="password", database="emp")
# Function to mAdd_Employee
def Add_Employ():
menu()
Output: