import mysql.
connector
from datetime import datetime
def connect_to_mysql():
# Replace the placeholders with your MySQL server information
config = {
'user': 'your_username',
'password': 'your_password',
'host': 'localhost',
'database': 'your_database_name',
'raise_on_warnings': True,
try:
connection = mysql.connector.connect(**config)
print("Connected to MySQL database")
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
def menu():
print("enter 1 to book a ticket")
print("enter 2 to acesss railway canteen")
print("enter 3 to update booking status")
print(“enter 5 to print your ticket”)
n=int(input("enter a number"))
if n==1:
bookticket(passengername,source,destination)
elif n==2:
print_ticket(ticket_id):
elif n==3:
update_ticket(ticket_id, new_passenger_name=None, new_destination=None):
elif n==5:
view_canteen_menu()
def book_ticket(passenger_name, source, destination):
# Connect to MySQL
connection = connect_to_mysql()
if connection is not None:
try:
# Create a cursor object
cursor = connection.cursor()
# Get the current date and time
booking_date = datetime.now()
# Insert ticket information into the database
query = "INSERT INTO tickets (passenger_name, source, destination, booking_date) VALUES
(%s, %s, %s, %s)"
values = (passenger_name, source, destination, booking_date)
cursor.execute(query, values)
# Commit the changes
connection.commit()
print("Ticket booked successfully!")
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
cursor.close()
connection.close()
print("MySQL connection closed")
def print_ticket(ticket_id):
# Connect to MySQL
connection = connect_to_mysql()
if connection is not None:
try:
# Create a cursor object
cursor = connection.cursor(dictionary=True)
# Retrieve ticket information from the database
query = "SELECT * FROM tickets WHERE ticket_id = %s"
values = (ticket_id,)
cursor.execute(query, values)
# Fetch the ticket details
ticket = cursor.fetchone()
if ticket:
# Format and print the ticket
print("Ticket Information:")
print(f"Ticket ID: {ticket['ticket_id']}")
print(f"Passenger Name: {ticket['passenger_name']}")
print(f"Source: {ticket['source']}")
print(f"Destination: {ticket['destination']}")
print(f"Booking Date: {ticket['booking_date']}")
else:
print(f"No ticket found with Ticket ID: {ticket_id}")
def update_ticket(ticket_id, new_passenger_name=None, new_destination=None):
# Connect to MySQL
connection = connect_to_mysql()
if connection is not None:
try:
# Create a cursor object
cursor = connection.cursor()
# Construct the UPDATE query based on the provided information
update_query = "UPDATE tickets SET "
update_values = []
if new_passenger_name:
update_query += "passenger_name = %s, "
update_values.append(new_passenger_name)
if new_destination:
update_query += "destination = %s, "
update_values.append(new_destination)
# Remove the trailing comma and space
update_query = update_query.rstrip(", ")
# Add the WHERE clause for the specific ticket ID
update_query += " WHERE ticket_id = %s"
update_values.append(ticket_id)
# Execute the UPDATE query
cursor.execute(update_query, update_values)
# Commit the changes
connection.commit()
print(f"Ticket with ID {ticket_id} updated successfully!")
def view_canteen_menu():
# Connect to MySQL
connection = connect_to_mysql()
if connection is not None:
try:
# Create a cursor object
cursor = connection.cursor(dictionary=True)
# Retrieve canteen menu items from the database
query = "SELECT * FROM canteen_menu"
cursor.execute(query)
# Fetch all menu items
menu_items = cursor.fetchall()
if menu_items:
print("Canteen Menu:")
for item in menu_items:
print(f"{item['item_id']}. {item['item_name']} - ${item['item_price']}")
else:
print("No items available in the canteen menu.")
sql table:
canteen_menu:
Tickets: