0% found this document useful (0 votes)
0 views

import 12

Uploaded by

harsha26122007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

import 12

Uploaded by

harsha26122007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

import mysql.

connector as mc

# Establishing the database connection


mdb = mc.connect(host='localhost', user='root', passwd='sujith123',
database='db1234')
mcur = mdb.cursor()

if mdb.is_connected():
print("Connected to the database.")
else:
print("Failed to connect to the database.")

# Create tables if they don't exist


def createtable():
q1 = """CREATE TABLE IF NOT EXISTS passengers (
Name VARCHAR(20),
Age INT,
Seat_no INT PRIMARY KEY,
Destination VARCHAR(20),
No_bag CHAR(1),
Wt_lug CHAR(2)
)"""
mcur.execute(q1)

q2 = """CREATE TABLE IF NOT EXISTS flights (


Starting_from VARCHAR(20),
Going_to VARCHAR(20),
Takeoffdate DATETIME,
Land_date DATETIME,
Type VARCHAR(13)
)"""
mcur.execute(q2)

q3 = """CREATE TABLE IF NOT EXISTS Cake (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q3)

q4 = """CREATE TABLE IF NOT EXISTS Mocktail (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q4)

q5 = """CREATE TABLE IF NOT EXISTS Coffee (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q5)

q6 = """CREATE TABLE IF NOT EXISTS Tea (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q6)

q7 = """CREATE TABLE IF NOT EXISTS Meal (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q7)

q8 = """CREATE TABLE IF NOT EXISTS Snacks (


Srno INT PRIMARY KEY,
Type VARCHAR(15),
Price VARCHAR(3)
)"""
mcur.execute(q8)

q13 = """CREATE TABLE IF NOT EXISTS forder (


Srno INT PRIMARY KEY,
Name VARCHAR(20),
Item VARCHAR(15)
)"""
mcur.execute(q13)

print("Tables created successfully.")

# Function to insert sample passengers


def insert_sample_passengers():
passengers_data = [
('Alice', 25, 101, 'Paris', '2', '20'),
('Bob', 30, 102, 'London', '1', '15'),
('Charlie', 35, 103, 'Paris', '3', '25'),
('David', 28, 104, 'New York', '2', '18'),
('Eve', 22, 105, 'London', '1', '12')
]
query = "INSERT INTO passengers (Name, Age, Seat_no, Destination, No_bag,
Wt_lug) VALUES (%s, %s, %s, %s, %s, %s)"
try:
mcur.executemany(query, passengers_data)
mdb.commit()
print("Sample passengers inserted.")
except mc.Error as err:
print(f"Error: {err}")
mdb.rollback()

# Function to insert sample flights


def insert_sample_flights():
flights_data = [
('Mumbai', 'Paris', '2023-12-26 10:00:00', '2023-12-26 14:00:00',
'International'),
('Delhi', 'London', '2023-12-27 12:00:00', '2023-12-27 16:00:00',
'International'),
('Bangalore', 'New York', '2023-12-28 11:00:00', '2023-12-28 15:00:00',
'International'),
('Chennai', 'Dubai', '2023-12-29 09:00:00', '2023-12-29 13:00:00',
'International'),
('Kolkata', 'Singapore', '2023-12-30 08:00:00', '2023-12-30 12:00:00',
'International')
]
query = "INSERT INTO flights (Starting_from, Going_to, Takeoffdate, Land_date,
Type) VALUES (%s, %s, %s, %s, %s)"
try:
mcur.executemany(query, flights_data)
mdb.commit()
print("Sample flights inserted.")
except mc.Error as err:
print(f"Error: {err}")
mdb.rollback()

# Function to add a new passenger


def addpassenger():
name = input("Enter passenger's name: ")
age = input("Enter age: ")
seat_no = int(input("Enter alloted seat number: "))
destination = input("Enter destination: ")
wtallowed = 5 # max weight allowed for luggage
nobag = int(input("Enter number of bags: "))
wtbag = int(input("Enter weight of luggage (kg): "))

if wtbag < wtallowed:


print("Luggage weight is within the limit.")
else:
print("Your luggage is overweight...")

q9 = "INSERT INTO passengers (Name, Age, Seat_no, Destination, No_bag, Wt_lug)


VALUES ('{}', {}, {}, '{}', {}, {})".format(name, age, seat_no, destination, nobag,
wtbag)
try:
mcur.execute(q9)
mdb.commit()
print("Passenger added successfully.")
except mc.Error as err:
print(f"Error: {err}")
mdb.rollback()

# Function for food ordering


def food():
print("1. Cake")
print("2. Mocktail")
print("3. Coffee")
print("4. Tea")
print("5. Meal")
print("6. Snacks")
choice = int(input("Enter your choice: "))

if choice == 1:
q10 = "SELECT * FROM Cake"
mcur.execute(q10)
d1 = mcur.fetchall()
print(d1)
elif choice == 2:
q11 = "SELECT * FROM Mocktail"
mcur.execute(q11)
d2 = mcur.fetchall()
print(d2)
elif choice == 3:
q12 = "SELECT * FROM Coffee"
mcur.execute(q12)
d3 = mcur.fetchall()
print(d3)
elif choice == 4:
q13 = "SELECT * FROM Tea"
mcur.execute(q13)
d4 = mcur.fetchall()
print(d4)
elif choice == 5:
q14 = "SELECT * FROM Meal"
mcur.execute(q14)
d5 = mcur.fetchall()
print(d5)
elif choice == 6:
q15 = "SELECT * FROM Snacks"
mcur.execute(q15)
d6 = mcur.fetchall()
print(d6)
else:
print("Invalid choice.")

while True:
srno = int(input("Enter srno: "))
name = input("Enter your name: ")
item = input("Enter order: ")
q16 = "INSERT INTO forder (Srno, Name, Item) VALUES (%s, %s, %s)"
mcur.execute(q16, (srno, name, item))
mdb.commit()
print("Order placed.")

more = input("Do you want to order more (y/n): ")


if more.lower() == 'n':
break

# Functions to display available data


def avail_flights():
q16 = "SELECT Starting_from, Going_to FROM flights"
mcur.execute(q16)
flights = mcur.fetchall()
if flights:
print("From ---> To")
for flight in flights:
print(flight)
else:
print("No flights available.")

def display_passengers():
q17 = "SELECT * FROM passengers"
mcur.execute(q17)
passengers = mcur.fetchall()
print("Passengers Details:")
for passenger in passengers:
print(passenger)

def passengers_goingto():
q18 = "SELECT Name, Destination FROM passengers ORDER BY Destination"
mcur.execute(q18)
passengers = mcur.fetchall()
if passengers:
print("Passengers Going to Common Destinations:")
for passenger in passengers:
print(passenger)
else:
print("No passengers found.")

# Initial Setup: Create tables and insert sample data


createtable()
insert_sample_passengers()
insert_sample_flights()

# Main Menu Loop


while True:
print("\n1. Add passenger")
print("2. Flights available")
print("3. To display all passengers")
print("4. Passengers going to common place")
print("5. Food ordering")
print("6. Exit")

ch = int(input("Enter your choice: "))


if ch == 1:
addpassenger()
elif ch == 2:
avail_flights()
elif ch == 3:
display_passengers()
elif ch == 4:
passengers_goingto()
elif ch == 5:
food()
elif ch == 6:
c = input("Do you want to exit (y/n): ")
if c.lower() == 'y':
print("Thanks for visiting!")
break
else:
print("Invalid choice.")

You might also like