Creating a Hotel Management System using MySQL and Python with Pandas
involves several steps. Below is a simplified version of such a project that allows you
to manage hotel reservations, guests, rooms, and payments using MySQL as the
database and Pandas for data manipulation and analysis.
Steps:
1. Set up the MySQL Database: Create a MySQL database and
necessary tables.
2. Connect Python with MySQL: Use the mysql-connector
library to interact with the database.
3. Create a Hotel Management System Interface: Use Python to
create an interface for managing reservations, guests, and payments.
4. Use Pandas for Data Analysis: Pandas will be used to manipulate
and analyze data, such as checking availability, calculating earnings,
etc.
1. Setting up MySQL Database
First, create a MySQL database named hotel_management_system and the
necessary tables.
CREATE DATABASE hotel_management_system;
USE hotel_management_system;
CREATE TABLE rooms (
room_id INT PRIMARY KEY,
room_type VARCHAR(50),
capacity INT,
price DECIMAL(10, 2),
status VARCHAR(20)
);
CREATE TABLE guests (
guest_id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100),
phone VARCHAR(15),
address VARCHAR(255)
);
CREATE TABLE reservations (
reservation_id INT AUTO_INCREMENT PRIMARY KEY,
guest_id INT,
room_id INT,
check_in_date DATE,
check_out_date DATE,
total_amount DECIMAL(10, 2),
FOREIGN KEY (guest_id) REFERENCES guests(guest_id),
FOREIGN KEY (room_id) REFERENCES rooms(room_id)
);
CREATE TABLE payments (
payment_id INT AUTO_INCREMENT PRIMARY KEY,
reservation_id INT,
amount_paid DECIMAL(10, 2),
payment_date DATE,
FOREIGN KEY (reservation_id) REFERENCES reservations(reservation_id)
);
output Setting up MySQL Database
2. Connecting Python with MySQL
Install mysql-connector using pip if it's not installed:
pip install mysql-connector-python
Now, use Python to connect to the MySQL database:
import mysql.connector
import pandas as pd
# Establish a connection to the MySQL database
connection = mysql.connector.connect(
host="localhost",
user="root",
password="password", # Replace with your MySQL password
database="hotel_management_system"
)
cursor = connection.cursor()
3. Functions for Hotel Management System
We will create Python functions to manage reservations, rooms, and payments.
Function to Add a Room:
def add_room(room_id, room_type, capacity, price, status):
query = "INSERT INTO rooms (room_id, room_type, capacity, price, status)
VALUES (%s, %s, %s, %s, %s)"
values = (room_id, room_type, capacity, price, status)
cursor.execute(query, values)
connection.commit()
print(f"Room {room_id} added successfully.")
output Function to Add a Room:
Function to Add a Guest:
def add_guest(first_name, last_name, email, phone, address):
query = "INSERT INTO guests (first_name, last_name, email, phone, address)
VALUES (%s, %s, %s, %s, %s)"
values = (first_name, last_name, email, phone, address)
cursor.execute(query, values)
connection.commit()
print(f"Guest {first_name} {last_name} added successfully.")
output Function to Add a Guest:
Function to Make a Reservation:
def make_reservation(guest_id, room_id, check_in_date, check_out_date):
# Calculate total amount based on room price
query = f"SELECT price FROM rooms WHERE room_id = {room_id}"
cursor.execute(query)
room_price = cursor.fetchone()[0]
total_amount = room_price * (pd.to_datetime(check_out_date) -
pd.to_datetime(check_in_date)).days
# Insert reservation into the database
query = "INSERT INTO reservations (guest_id, room_id, check_in_date,
check_out_date, total_amount) VALUES (%s, %s, %s, %s, %s)"
values = (guest_id, room_id, check_in_date, check_out_date, total_amount)
cursor.execute(query, values)
connection.commit()
print(f"Reservation made for guest {guest_id} in room {room_id} from
{check_in_date} to {check_out_date}.")
output Function to Make a Reservation:
Function to Record Payment:
def record_payment(reservation_id, amount_paid, payment_date):
query = "INSERT INTO payments (reservation_id, amount_paid, payment_date)
VALUES (%s, %s, %s)"
values = (reservation_id, amount_paid, payment_date)
cursor.execute(query, values)
connection.commit()
print(f"Payment of {amount_paid} recorded for reservation
{reservation_id}.")
output Function to Record Payment:
4. Data Analysis Using Pandas
Pandas can be used to analyze the data in the database. Here's an example of how to calculate total
earnings from reservations:
def calculate_total_earnings():
# Fetch payment data from the database
query = "SELECT r.room_type, SUM(p.amount_paid) as total_earnings FROM
payments p JOIN reservations r ON p.reservation_id = r.reservation_id GROUP BY
r.room_type"
cursor.execute(query)
data = cursor.fetchall()
# Convert to a pandas DataFrame for analysis
df = pd.DataFrame(data, columns=["Room Type", "Total Earnings"])
print(df)
output Data Analysis Using Pandas
5. Example of Usage
# Example of adding a room
add_room(101, "Single", 1, 100.0, "Available")
add_room(102, "Double", 2, 150.0, "Available")
# Example of adding a guest
add_guest("John", "Doe", "
[email protected]", "1234567890", "123 Main St,
City, Country")
# Example of making a reservation
make_reservation(1, 101, "2024-12-20", "2024-12-25")
# Example of recording a payment
record_payment(1, 500.0, "2024-12-20")
# Example of calculating total earnings
calculate_total_earnings()
output Example of Usage
Output Example:
DataFrame Output of Earnings:
Room Type | Total Earnings
---------------------------
Single | 500.0
Double | 0.0
output DataFrame Output of Earnings:
This is a basic example to get you started on a Hotel Management System. You can extend it by
adding more features such as user interfaces (using Tkinter or Flask for web apps), room
availability checking, report generation, etc. You can also integrate more advanced analytics with
Pandas, like calculating occupancy rates, analyzing booking trends, and so on.