0% found this document useful (0 votes)
314 views5 pages

Movie Ticket Booking System

Python integrated with MySQL

Uploaded by

Pavalan SOLO
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)
314 views5 pages

Movie Ticket Booking System

Python integrated with MySQL

Uploaded by

Pavalan SOLO
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/ 5

Database Schema

Run the following SQL commands to create the tables:

CREATE DATABASE movie_booking;

USE movie_booking;

CREATE TABLE movies (


id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
available_seats INT DEFAULT 100
);

CREATE TABLE bookings (


booking_id INT AUTO_INCREMENT PRIMARY KEY,
movie_id INT,
seats_booked INT,
booking_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (movie_id) REFERENCES movies(id)
);

Sample Movies Data

You can insert some sample movie data to work with:

INSERT INTO movies (title, available_seats) VALUES ('Inception', 100);


INSERT INTO movies (title, available_seats) VALUES ('Avatar', 150);
INSERT INTO movies (title, available_seats) VALUES ('Interstellar', 80);

Python Code for Movie Ticket Booking System

Here’s the code, including connection setup, displaying available movies, booking tickets, and handling the
MySQL database interactions.

import mysql.connector
from datetime import datetime

# Step 1: Connect to MySQL database


def connect_db():
return mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="movie_booking"
)

# Step 2: Show available movies


def show_movies():
db = connect_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM movies")
movies = cursor.fetchall()
db.close()

print("Available Movies:")
for movie in movies:
print(f"ID: {movie[0]}, Title: {movie[1]}, Available Seats: {movie[2]}")
# Step 3: Book tickets for a movie
def book_ticket():
show_movies()
movie_id = int(input("\nEnter the Movie ID to book tickets for: "))
seats = int(input("Enter the number of seats you want to book: "))

db = connect_db()
cursor = db.cursor()

# Check availability
cursor.execute("SELECT available_seats FROM movies WHERE id = %s", (movie_id,))
available_seats = cursor.fetchone()
if available_seats and seats <= available_seats[0]:
# Update available seats
new_seat_count = available_seats[0] - seats
cursor.execute("UPDATE movies SET available_seats = %s WHERE id = %s",
(new_seat_count, movie_id))

# Add booking record


cursor.execute("INSERT INTO bookings (movie_id, seats_booked) VALUES (%s, %s)",
(movie_id, seats))
db.commit()

print("\nBooking Confirmed!")
print(f"Seats Booked: {seats} for Movie ID: {movie_id}")
else:
print("\nSorry, not enough seats available.")

db.close()

# Step 4: View booking history


def view_bookings():
db = connect_db()
cursor = db.cursor()
cursor.execute("""
SELECT b.booking_id, m.title, b.seats_booked, b.booking_date
FROM bookings b JOIN movies m ON b.movie_id = m.id
ORDER BY b.booking_date DESC
""")
bookings = cursor.fetchall()
db.close()

print("\nBooking History:")
for booking in bookings:
print(f"Booking ID: {booking[0]}, Movie: {booking[1]}, Seats Booked:
{booking[2]}, Date: {booking[3]}")

# Step 5: Main menu


def main():
while True:
print("\n--- Movie Ticket Booking System ---")
print("1. Show Available Movies")
print("2. Book Tickets")
print("3. View Booking History")
print("4. Exit")
choice = input("Enter your choice: ")

if choice == '1':
show_movies()
elif choice == '2':
book_ticket()
elif choice == '3':
view_bookings()
elif choice == '4':
print("Thank you for using the Movie Ticket Booking System.")
break
else:
print("Invalid choice, please try again.")

if __name__ == "__main__":
main()

Explanation of Code

1. connect_db(): Establishes a connection to the MySQL database.


2. show_movies(): Fetches and displays available movies and seat counts.
3. book_ticket(): Prompts the user for a movie ID and the number of seats to book. It checks seat
availability, updates the seat count, and adds a booking record.
4. view_bookings(): Fetches and displays booking history, showing each booking’s details.
5. main(): Displays a menu to the user, allowing them to view movies, book tickets, or check booking
history.
Sample Output

No.1
--- Movie Ticket Booking System ---
1. Show Available Movies
2. Book Tickets
3. View Booking History
4. Exit
Enter your choice: 1

Available Movies:
ID: 1, Title: Inception, Available Seats: 100
ID: 2, Title: Avatar, Available Seats: 150
ID: 3, Title: Interstellar, Available Seats: 80

No.2
--- Movie Ticket Booking System ---
1. Show Available Movies
2. Book Tickets
3. View Booking History
4. Exit
Enter your choice: 2

Available Movies:
ID: 1, Title: Inception, Available Seats: 100
ID: 2, Title: Avatar, Available Seats: 150
ID: 3, Title: Interstellar, Available Seats: 80

Enter the Movie ID to book tickets for: 2


Enter the number of seats you want to book: 5

Booking Confirmed!
Seats Booked: 5 for Movie ID: 2

No.3
--- Movie Ticket Booking System ---
1. Show Available Movies
2. Book Tickets
3. View Booking History
4. Exit
Enter your choice: 3

Booking History:
Booking ID: 1, Movie: Avatar, Seats Booked: 5, Date: 2024-11-12 15:23:45

No.4
--- Movie Ticket Booking System ---
1. Show Available Movies
2. Book Tickets
3. View Booking History
4. Exit
Enter your choice: 2

Available Movies:
ID: 1, Title: Inception, Available Seats: 100
ID: 2, Title: Avatar, Available Seats: 145
ID: 3, Title: Interstellar, Available Seats: 80
Enter the Movie ID to book tickets for: 3
Enter the number of seats you want to book: 85

Sorry, not enough seats available.

No.5
--- Movie Ticket Booking System ---
1. Show Available Movies
2. Book Tickets
3. View Booking History
4. Exit
Enter your choice: 4

Thank you for using the Movie Ticket Booking System.

Explanation of Sample Output

1. Show Available Movies: Lists all movies with IDs, titles, and available seats.
2. Book Tickets: Allows the user to book seats if enough are available and confirms the booking.
3. View Booking History: Displays all previous bookings with relevant details.
4. Exit: Exits the program with a farewell message.

You might also like