Category: python
Employee Management System - Project Setup
Published on 14 Aug 2026
Explanation
The Employee Management System is a practical Python project that combines core Python concepts with MySQL database connectivity. The system will manage employee records using CRUD operations: Create, Read, Update, and Delete. The project can be structured into separate modules for database connectivity, employee operations, and the main application. First, install the MySQL connector package and create a MySQL database.
Code:
# Install MySQL connector
# pip install mysql-connector-python
# MySQL Database
CREATE DATABASE company;
USE company;
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(150),
department VARCHAR(100),
salary DECIMAL(10, 2)
);
Explanation
The mysql-connector-python package allows Python applications to communicate with MySQL. A database connection is created using mysql.connector.connect() with the MySQL host, username, password, and database name. A cursor is then created to execute SQL queries. Database credentials should ideally be stored in environment variables instead of hardcoding them in the source code.
Code:
import mysql.connector
def get_connection():
return mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='company'
)
connection = get_connection()
if connection.is_connected():
print('Connected to MySQL successfully')
connection.close()
Explanation
The Create and Read operations allow users to add new employees and view existing employee records. Parameterized queries are used instead of directly concatenating values into SQL statements. This protects the application against SQL injection and handles values safely. The commit() method saves the INSERT operation permanently. The SELECT query retrieves employee records, which can then be displayed using a Python loop.
Code:
import mysql.connector
def get_connection():
return mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='company'
)
def add_employee(name, email, department, salary):
connection = get_connection()
cursor = connection.cursor()
query = '''
INSERT INTO employees
(name, email, department, salary)
VALUES (%s, %s, %s, %s)
'''
cursor.execute(query, (name, email, department, salary))
connection.commit()
cursor.close()
connection.close()
print('Employee added successfully')
def view_employees():
connection = get_connection()
cursor = connection.cursor()
cursor.execute('SELECT * FROM employees')
for employee in cursor.fetchall():
print(employee)
cursor.close()
connection.close()
add_employee('John', 'john@example.com', 'IT', 50000)
view_employees()
Explanation
The Update and Delete operations allow users to modify or remove existing employee records. UPDATE changes specific fields based on the employee ID, while DELETE removes the selected employee. Parameterized queries should be used for both operations. After executing these queries, connection.commit() saves the changes. In a production application, these operations should also include validation and exception handling.
Code:
import mysql.connector
def get_connection():
return mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='company'
)
def update_employee(employee_id, salary, department):
connection = get_connection()
cursor = connection.cursor()
query = '''
UPDATE employees
SET salary = %s, department = %s
WHERE id = %s
'''
cursor.execute(query, (salary, department, employee_id))
connection.commit()
print('Employee updated successfully')
cursor.close()
connection.close()
def delete_employee(employee_id):
connection = get_connection()
cursor = connection.cursor()
cursor.execute(
'DELETE FROM employees WHERE id = %s',
(employee_id,)
)
connection.commit()
print('Employee deleted successfully')
cursor.close()
connection.close()
update_employee(1, 60000, 'Engineering')
delete_employee(1)
Explanation
The final application combines the database operations into a menu-driven Employee Management System. Users can choose to add, view, update, or delete employees. Functions are used to separate responsibilities and make the application easier to maintain. The project can later be improved by adding exception handling, input validation, logging, unit testing, search functionality, pagination, authentication, and a REST API using FastAPI.
Code:
import mysql.connector
def get_connection():
return mysql.connector.connect(
host='localhost',
user='root',
password='password',
database='company'
)
def add_employee():
name = input('Name: ')
email = input('Email: ')
department = input('Department: ')
salary = float(input('Salary: '))
connection = get_connection()
cursor = connection.cursor()
cursor.execute('''
INSERT INTO employees
(name, email, department, salary)
VALUES (%s, %s, %s, %s)
''', (name, email, department, salary))
connection.commit()
cursor.close()
connection.close()
print('Employee added successfully')
def view_employees():
connection = get_connection()
cursor = connection.cursor()
cursor.execute('SELECT * FROM employees')
for employee in cursor.fetchall():
print(employee)
cursor.close()
connection.close()
while True:
print('\nEmployee Management System')
print('1. Add Employee')
print('2. View Employees')
print('3. Exit')
choice = input('Enter your choice: ')
if choice == '1':
add_employee()
elif choice == '2':
view_employees()
elif choice == '3':
print('Application closed')
break
else:
print('Invalid choice')