PROGRAM
PROGRAM
import mysql.connector
from mysql.connector import Error
def create_connection():
"""Create a database connection to the MySQL database."""
conn = None
try:
conn = mysql.connector.connect(
host=host,
user=username,
password=password
)
print("Connection to MySQL DB successful")
except Error as e:
print(f"The error '{e}' occurred")
return conn
def create_database(conn):
"""Create a database for the party hall booking system."""
cursor = conn.cursor()
try:
cursor.execute(f"CREATE DATABASE IF NOT EXISTS
{database}")
print(f"Database '{database}' created successfully")
except Error as e:
print(f"The error '{e}' occurred")
def create_tables(conn):
"""Create tables for hall owners and bookings."""
cursor = conn.cursor()
cursor.execute(f"USE {database}")
cursor.execute(create_owners_table)
cursor.execute(create_bookings_table)
print("Tables created successfully")
def list_halls(conn):
"""List all available halls, filtered by location."""
cursor = conn.cursor()
cursor.execute(f"USE {database}")
if halls:
print("Available Party Halls:")
for hall in halls:
print(f"ID: {hall[0]}, Hall Name: {hall[4]}, Location: {hall[5]},
Capacity: {hall[6]}, Price: ${hall[7]:.2f}")
else:
print("No halls available in the specified location.")
return halls # Return the list of halls
def add_hall_owner(conn):
"""Add a new hall owner."""
cursor = conn.cursor()
cursor.execute(f"USE {database}")
insert_query = """
INSERT INTO owners (name, email, phone, hall_name, hall_location,
hall_capacity, hall_price)
VALUES (%s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(insert_query, (name, email, phone, hall_name,
hall_location, hall_capacity, hall_price))
conn.commit()
print("Hall owner added successfully.")
def book_hall(conn):
"""Book a hall for a customer."""
cursor = conn.cursor()
cursor.execute(f"USE {database}")
if hall_price is None:
print("Invalid hall ID. Booking failed.")
return
hall_price = hall_price[0]
payment = hall_price
insert_query = """
INSERT INTO bookings (owner_id, customer_name, customer_email,
booking_date, booking_time, guests, payment, location)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(insert_query, (hall_id, customer_name,
customer_email, booking_date, booking_time, guests, payment,
location))
conn.commit()
print("Booking successful!")
generate_receipt(hall_id, customer_name, customer_email,
booking_date, booking_time, guests, payment)
def main():
conn = create_connection()
create_database(conn)
create_tables(conn)
while True:
print("\n1. Add Hall Owner")
print("2. List Halls")
print("3. Book Hall")
print("4. Exit")
choice = input("Choose an option: ")
if choice == '1':
add_hall_owner(conn)
elif choice == '2':
list_halls(conn)
elif choice == '3':
book_hall(conn)
elif choice == '4':
break
else:
print("Invalid choice, please try again.")
conn.close()
if __name__ == "__main__":
main()
OUTPUT
Adding a new hall owner: