# Establish database connection
def connect_to_db():
try:
obj
= mysql.connector
.connect
( host="localhost",
user="root",
password="admin"
)
return obj
except
mysql.connector
.Error
as err
: print(f"Error connecting to MySQL: {err}")
# Create database and tables
def setup_database(mycursor):
mycursor.execute("CREATE DATABASE IF NOT EXISTS airlines")
mycursor.execute("USE airlines")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS food_items (
sl_no INT AUTO_INCREMENT PRIMARY KEY,
food_name VARCHAR(40) NOT NULL,
price INT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS luggage (
luggage_id INT AUTO_INCREMENT PRIMARY KEY,
weight INT NOT NULL,
price INT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS cust_details (
cust_id INT AUTO_INCREMENT PRIMARY KEY,
cust_name VARCHAR(40) NOT NULL,
cont_no BIGINT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS flight_details (
flight_id INT AUTO_INCREMENT PRIMARY KEY,
cus_id INT,
cus_name VARCHAR(40) NOT NULL,
departure VARCHAR(50),
destination VARCHAR(50),
flight_day DATE,
flight_time TIME,
FOREIGN KEY (cus_id) REFERENCES cust_details(cust_id)
)
""")
# Insert sample data into food_items
def insert_sample_data(mycursor, obj):
try:
sample_data = [
('Pepsi', 150),
('Coffee', 70),
('Tea', 50),
('Water', 60),
('Milk Shake', 80),
('Chicken Burger', 160),
('Cheese Pizza', 70),
('Chicken Biryani', 300),
('Plane Rice', 80),
('Aloo Paratha', 120),
('Roti Sabji', 100),
('Omelette', 50)
]
mycursor.executemany("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", sample_data)
obj.commit()
print("Sample data inserted successfully.")
except
mysql.connector
.Error
as err
: print(f"Error inserting data: {err}")
# Admin functionalities
def admin(mycursor, obj):
while True:
print("\n--- Admin Menu ---")
print("1. View all data")
print("2. Manage food items")
print("3. Logout")
choice = input("Enter your choice: ")
if choice == "1":
mycursor.execute("SELECT * FROM food_items")
results = mycursor.fetchall()
for row in results:
print(row)
elif choice == "2":
manage_food_items(mycursor, obj)
elif choice == "3":
print("Logging out...")
break
else:
print("Invalid choice. Try again.")
# Manage food items
def manage_food_items(mycursor, obj):
while True:
print("\n--- Manage Food Items ---")
print("1. Add new food item")
print("2. Update food item price")
print("3. Delete food item")
print("4. Go back")
choice = input("Enter your choice: ")
if choice == "1":
food_name = input("Enter food name: ")
price = int(input("Enter food price: "))
try:
mycursor.execute("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", (food_name, price))
obj.commit()
print("Food item added successfully.")
except
mysql.connector
.Error
as err
: print(f"Error: {err}")
elif choice == "2":
sl_no = int(input("Enter food item ID to update: "))
price = int(input("Enter new price: "))
try:
mycursor.execute("UPDATE food_items SET price = %s WHERE sl_no = %s", (price, sl_no))
obj.commit()
print("Price updated successfully.")
except
mysql.connector
.Error
as err
: print(f"Error: {err}")
elif choice == "3":
sl_no = int(input("Enter food item ID to delete: "))
try:
mycursor.execute("DELETE FROM food_items WHERE sl_no = %s", (sl_no,))
obj.commit()
print("Food item deleted successfully.")
except
mysql.connector
.Error
as err
: print(f"Error: {err}")
elif choice == "4":
break
else:
print("Invalid choice. Try again.")
# User functionalities
def user(mycursor):
while True:
print("\n--- User Menu ---")
print("1. View available food items")
print("2. Logout")
choice = input("Enter your choice: ")
if choice == "1":
mycursor.execute("SELECT * FROM food_items")
results = mycursor.fetchall()
print("\nAvailable Food Items:")
for row in results:
print(f"ID: {row[0]}, Name: {row[1]}, Price: {row[2]}")
elif choice == "2":
print("Logging out...")
break
else:
print("Invalid choice. Try again.")
# Main menu
def main_menu(mycursor, obj):
while True:
print("\n--- Main Menu ---")
print("1. Admin")
print("2. User")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
password = input("Enter admin password: ")
if password == "admin":
admin(mycursor, obj)
else:
print("Incorrect password.")
elif choice == "2":
user(mycursor)
elif choice == "3":
print("Exiting the application.")
break
else:
print("Invalid choice. Try again.")
# Run the program
if __name__ == "__main__":
connection = connect_to_db()
cursor = connection.cursor()
setup_database(cursor)
insert_sample_data(cursor, connection)
main_menu(cursor, connection)
connection.close()
aW1wb3J0IG15c3FsLmNvbm5lY3RvcgoKIyBFc3RhYmxpc2ggZGF0YWJhc2UgY29ubmVjdGlvbgpkZWYgY29ubmVjdF90b19kYigpOgogICAgdHJ5OgogICAgICAgIG9iaiA9IG15c3FsLmNvbm5lY3Rvci5jb25uZWN0KAogICAgICAgICAgICBob3N0PSJsb2NhbGhvc3QiLAogICAgICAgICAgICB1c2VyPSJyb290IiwKICAgICAgICAgICAgcGFzc3dvcmQ9ImFkbWluIgogICAgICAgICkKICAgICAgICByZXR1cm4gb2JqCiAgICBleGNlcHQgbXlzcWwuY29ubmVjdG9yLkVycm9yIGFzIGVycjoKICAgICAgICBwcmludChmIkVycm9yIGNvbm5lY3RpbmcgdG8gTXlTUUw6IHtlcnJ9IikKICAgICAgICBleGl0KCkKCiMgQ3JlYXRlIGRhdGFiYXNlIGFuZCB0YWJsZXMKZGVmIHNldHVwX2RhdGFiYXNlKG15Y3Vyc29yKToKICAgIG15Y3Vyc29yLmV4ZWN1dGUoIkNSRUFURSBEQVRBQkFTRSBJRiBOT1QgRVhJU1RTIGFpcmxpbmVzIikKICAgIG15Y3Vyc29yLmV4ZWN1dGUoIlVTRSBhaXJsaW5lcyIpCgogICAgbXljdXJzb3IuZXhlY3V0ZSgiIiIKICAgICAgICBDUkVBVEUgVEFCTEUgSUYgTk9UIEVYSVNUUyBmb29kX2l0ZW1zICgKICAgICAgICAgICAgc2xfbm8gSU5UIEFVVE9fSU5DUkVNRU5UIFBSSU1BUlkgS0VZLAogICAgICAgICAgICBmb29kX25hbWUgVkFSQ0hBUig0MCkgTk9UIE5VTEwsCiAgICAgICAgICAgIHByaWNlIElOVCBOT1QgTlVMTAogICAgICAgICkKICAgICIiIikKCiAgICBteWN1cnNvci5leGVjdXRlKCIiIgogICAgICAgIENSRUFURSBUQUJMRSBJRiBOT1QgRVhJU1RTIGx1Z2dhZ2UgKAogICAgICAgICAgICBsdWdnYWdlX2lkIElOVCBBVVRPX0lOQ1JFTUVOVCBQUklNQVJZIEtFWSwKICAgICAgICAgICAgd2VpZ2h0IElOVCBOT1QgTlVMTCwKICAgICAgICAgICAgcHJpY2UgSU5UIE5PVCBOVUxMCiAgICAgICAgKQogICAgIiIiKQoKICAgIG15Y3Vyc29yLmV4ZWN1dGUoIiIiCiAgICAgICAgQ1JFQVRFIFRBQkxFIElGIE5PVCBFWElTVFMgY3VzdF9kZXRhaWxzICgKICAgICAgICAgICAgY3VzdF9pZCBJTlQgQVVUT19JTkNSRU1FTlQgUFJJTUFSWSBLRVksCiAgICAgICAgICAgIGN1c3RfbmFtZSBWQVJDSEFSKDQwKSBOT1QgTlVMTCwKICAgICAgICAgICAgY29udF9ubyBCSUdJTlQgTk9UIE5VTEwKICAgICAgICApCiAgICAiIiIpCgogICAgbXljdXJzb3IuZXhlY3V0ZSgiIiIKICAgICAgICBDUkVBVEUgVEFCTEUgSUYgTk9UIEVYSVNUUyBmbGlnaHRfZGV0YWlscyAoCiAgICAgICAgICAgIGZsaWdodF9pZCBJTlQgQVVUT19JTkNSRU1FTlQgUFJJTUFSWSBLRVksCiAgICAgICAgICAgIGN1c19pZCBJTlQsCiAgICAgICAgICAgIGN1c19uYW1lIFZBUkNIQVIoNDApIE5PVCBOVUxMLAogICAgICAgICAgICBkZXBhcnR1cmUgVkFSQ0hBUig1MCksCiAgICAgICAgICAgIGRlc3RpbmF0aW9uIFZBUkNIQVIoNTApLAogICAgICAgICAgICBmbGlnaHRfZGF5IERBVEUsCiAgICAgICAgICAgIGZsaWdodF90aW1lIFRJTUUsCiAgICAgICAgICAgIEZPUkVJR04gS0VZIChjdXNfaWQpIFJFRkVSRU5DRVMgY3VzdF9kZXRhaWxzKGN1c3RfaWQpCiAgICAgICAgKQogICAgIiIiKQoKIyBJbnNlcnQgc2FtcGxlIGRhdGEgaW50byBmb29kX2l0ZW1zCmRlZiBpbnNlcnRfc2FtcGxlX2RhdGEobXljdXJzb3IsIG9iaik6CiAgICB0cnk6CiAgICAgICAgc2FtcGxlX2RhdGEgPSBbCiAgICAgICAgICAgICgnUGVwc2knLCAxNTApLAogICAgICAgICAgICAoJ0NvZmZlZScsIDcwKSwKICAgICAgICAgICAgKCdUZWEnLCA1MCksCiAgICAgICAgICAgICgnV2F0ZXInLCA2MCksCiAgICAgICAgICAgICgnTWlsayBTaGFrZScsIDgwKSwKICAgICAgICAgICAgKCdDaGlja2VuIEJ1cmdlcicsIDE2MCksCiAgICAgICAgICAgICgnQ2hlZXNlIFBpenphJywgNzApLAogICAgICAgICAgICAoJ0NoaWNrZW4gQmlyeWFuaScsIDMwMCksCiAgICAgICAgICAgICgnUGxhbmUgUmljZScsIDgwKSwKICAgICAgICAgICAgKCdBbG9vIFBhcmF0aGEnLCAxMjApLAogICAgICAgICAgICAoJ1JvdGkgU2FiamknLCAxMDApLAogICAgICAgICAgICAoJ09tZWxldHRlJywgNTApCiAgICAgICAgXQogICAgICAgIG15Y3Vyc29yLmV4ZWN1dGVtYW55KCJJTlNFUlQgSU5UTyBmb29kX2l0ZW1zIChmb29kX25hbWUsIHByaWNlKSBWQUxVRVMgKCVzLCAlcykiLCBzYW1wbGVfZGF0YSkKICAgICAgICBvYmouY29tbWl0KCkKICAgICAgICBwcmludCgiU2FtcGxlIGRhdGEgaW5zZXJ0ZWQgc3VjY2Vzc2Z1bGx5LiIpCiAgICBleGNlcHQgbXlzcWwuY29ubmVjdG9yLkVycm9yIGFzIGVycjoKICAgICAgICBwcmludChmIkVycm9yIGluc2VydGluZyBkYXRhOiB7ZXJyfSIpCgojIEFkbWluIGZ1bmN0aW9uYWxpdGllcwpkZWYgYWRtaW4obXljdXJzb3IsIG9iaik6CiAgICB3aGlsZSBUcnVlOgogICAgICAgIHByaW50KCJcbi0tLSBBZG1pbiBNZW51IC0tLSIpCiAgICAgICAgcHJpbnQoIjEuIFZpZXcgYWxsIGRhdGEiKQogICAgICAgIHByaW50KCIyLiBNYW5hZ2UgZm9vZCBpdGVtcyIpCiAgICAgICAgcHJpbnQoIjMuIExvZ291dCIpCgogICAgICAgIGNob2ljZSA9IGlucHV0KCJFbnRlciB5b3VyIGNob2ljZTogIikKICAgICAgICBpZiBjaG9pY2UgPT0gIjEiOgogICAgICAgICAgICBteWN1cnNvci5leGVjdXRlKCJTRUxFQ1QgKiBGUk9NIGZvb2RfaXRlbXMiKQogICAgICAgICAgICByZXN1bHRzID0gbXljdXJzb3IuZmV0Y2hhbGwoKQogICAgICAgICAgICBmb3Igcm93IGluIHJlc3VsdHM6CiAgICAgICAgICAgICAgICBwcmludChyb3cpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gIjIiOgogICAgICAgICAgICBtYW5hZ2VfZm9vZF9pdGVtcyhteWN1cnNvciwgb2JqKQogICAgICAgIGVsaWYgY2hvaWNlID09ICIzIjoKICAgICAgICAgICAgcHJpbnQoIkxvZ2dpbmcgb3V0Li4uIikKICAgICAgICAgICAgYnJlYWsKICAgICAgICBlbHNlOgogICAgICAgICAgICBwcmludCgiSW52YWxpZCBjaG9pY2UuIFRyeSBhZ2Fpbi4iKQoKIyBNYW5hZ2UgZm9vZCBpdGVtcwpkZWYgbWFuYWdlX2Zvb2RfaXRlbXMobXljdXJzb3IsIG9iaik6CiAgICB3aGlsZSBUcnVlOgogICAgICAgIHByaW50KCJcbi0tLSBNYW5hZ2UgRm9vZCBJdGVtcyAtLS0iKQogICAgICAgIHByaW50KCIxLiBBZGQgbmV3IGZvb2QgaXRlbSIpCiAgICAgICAgcHJpbnQoIjIuIFVwZGF0ZSBmb29kIGl0ZW0gcHJpY2UiKQogICAgICAgIHByaW50KCIzLiBEZWxldGUgZm9vZCBpdGVtIikKICAgICAgICBwcmludCgiNC4gR28gYmFjayIpCgogICAgICAgIGNob2ljZSA9IGlucHV0KCJFbnRlciB5b3VyIGNob2ljZTogIikKICAgICAgICBpZiBjaG9pY2UgPT0gIjEiOgogICAgICAgICAgICBmb29kX25hbWUgPSBpbnB1dCgiRW50ZXIgZm9vZCBuYW1lOiAiKQogICAgICAgICAgICBwcmljZSA9IGludChpbnB1dCgiRW50ZXIgZm9vZCBwcmljZTogIikpCiAgICAgICAgICAgIHRyeToKICAgICAgICAgICAgICAgIG15Y3Vyc29yLmV4ZWN1dGUoIklOU0VSVCBJTlRPIGZvb2RfaXRlbXMgKGZvb2RfbmFtZSwgcHJpY2UpIFZBTFVFUyAoJXMsICVzKSIsIChmb29kX25hbWUsIHByaWNlKSkKICAgICAgICAgICAgICAgIG9iai5jb21taXQoKQogICAgICAgICAgICAgICAgcHJpbnQoIkZvb2QgaXRlbSBhZGRlZCBzdWNjZXNzZnVsbHkuIikKICAgICAgICAgICAgZXhjZXB0IG15c3FsLmNvbm5lY3Rvci5FcnJvciBhcyBlcnI6CiAgICAgICAgICAgICAgICBwcmludChmIkVycm9yOiB7ZXJyfSIpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gIjIiOgogICAgICAgICAgICBzbF9ubyA9IGludChpbnB1dCgiRW50ZXIgZm9vZCBpdGVtIElEIHRvIHVwZGF0ZTogIikpCiAgICAgICAgICAgIHByaWNlID0gaW50KGlucHV0KCJFbnRlciBuZXcgcHJpY2U6ICIpKQogICAgICAgICAgICB0cnk6CiAgICAgICAgICAgICAgICBteWN1cnNvci5leGVjdXRlKCJVUERBVEUgZm9vZF9pdGVtcyBTRVQgcHJpY2UgPSAlcyBXSEVSRSBzbF9ubyA9ICVzIiwgKHByaWNlLCBzbF9ubykpCiAgICAgICAgICAgICAgICBvYmouY29tbWl0KCkKICAgICAgICAgICAgICAgIHByaW50KCJQcmljZSB1cGRhdGVkIHN1Y2Nlc3NmdWxseS4iKQogICAgICAgICAgICBleGNlcHQgbXlzcWwuY29ubmVjdG9yLkVycm9yIGFzIGVycjoKICAgICAgICAgICAgICAgIHByaW50KGYiRXJyb3I6IHtlcnJ9IikKICAgICAgICBlbGlmIGNob2ljZSA9PSAiMyI6CiAgICAgICAgICAgIHNsX25vID0gaW50KGlucHV0KCJFbnRlciBmb29kIGl0ZW0gSUQgdG8gZGVsZXRlOiAiKSkKICAgICAgICAgICAgdHJ5OgogICAgICAgICAgICAgICAgbXljdXJzb3IuZXhlY3V0ZSgiREVMRVRFIEZST00gZm9vZF9pdGVtcyBXSEVSRSBzbF9ubyA9ICVzIiwgKHNsX25vLCkpCiAgICAgICAgICAgICAgICBvYmouY29tbWl0KCkKICAgICAgICAgICAgICAgIHByaW50KCJGb29kIGl0ZW0gZGVsZXRlZCBzdWNjZXNzZnVsbHkuIikKICAgICAgICAgICAgZXhjZXB0IG15c3FsLmNvbm5lY3Rvci5FcnJvciBhcyBlcnI6CiAgICAgICAgICAgICAgICBwcmludChmIkVycm9yOiB7ZXJyfSIpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gIjQiOgogICAgICAgICAgICBicmVhawogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHByaW50KCJJbnZhbGlkIGNob2ljZS4gVHJ5IGFnYWluLiIpCgojIFVzZXIgZnVuY3Rpb25hbGl0aWVzCmRlZiB1c2VyKG15Y3Vyc29yKToKICAgIHdoaWxlIFRydWU6CiAgICAgICAgcHJpbnQoIlxuLS0tIFVzZXIgTWVudSAtLS0iKQogICAgICAgIHByaW50KCIxLiBWaWV3IGF2YWlsYWJsZSBmb29kIGl0ZW1zIikKICAgICAgICBwcmludCgiMi4gTG9nb3V0IikKCiAgICAgICAgY2hvaWNlID0gaW5wdXQoIkVudGVyIHlvdXIgY2hvaWNlOiAiKQogICAgICAgIGlmIGNob2ljZSA9PSAiMSI6CiAgICAgICAgICAgIG15Y3Vyc29yLmV4ZWN1dGUoIlNFTEVDVCAqIEZST00gZm9vZF9pdGVtcyIpCiAgICAgICAgICAgIHJlc3VsdHMgPSBteWN1cnNvci5mZXRjaGFsbCgpCiAgICAgICAgICAgIHByaW50KCJcbkF2YWlsYWJsZSBGb29kIEl0ZW1zOiIpCiAgICAgICAgICAgIGZvciByb3cgaW4gcmVzdWx0czoKICAgICAgICAgICAgICAgIHByaW50KGYiSUQ6IHtyb3dbMF19LCBOYW1lOiB7cm93WzFdfSwgUHJpY2U6IHtyb3dbMl19IikKICAgICAgICBlbGlmIGNob2ljZSA9PSAiMiI6CiAgICAgICAgICAgIHByaW50KCJMb2dnaW5nIG91dC4uLiIpCiAgICAgICAgICAgIGJyZWFrCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcHJpbnQoIkludmFsaWQgY2hvaWNlLiBUcnkgYWdhaW4uIikKCiMgTWFpbiBtZW51CmRlZiBtYWluX21lbnUobXljdXJzb3IsIG9iaik6CiAgICB3aGlsZSBUcnVlOgogICAgICAgIHByaW50KCJcbi0tLSBNYWluIE1lbnUgLS0tIikKICAgICAgICBwcmludCgiMS4gQWRtaW4iKQogICAgICAgIHByaW50KCIyLiBVc2VyIikKICAgICAgICBwcmludCgiMy4gRXhpdCIpCgogICAgICAgIGNob2ljZSA9IGlucHV0KCJFbnRlciB5b3VyIGNob2ljZTogIikKICAgICAgICBpZiBjaG9pY2UgPT0gIjEiOgogICAgICAgICAgICBwYXNzd29yZCA9IGlucHV0KCJFbnRlciBhZG1pbiBwYXNzd29yZDogIikKICAgICAgICAgICAgaWYgcGFzc3dvcmQgPT0gImFkbWluIjoKICAgICAgICAgICAgICAgIGFkbWluKG15Y3Vyc29yLCBvYmopCiAgICAgICAgICAgIGVsc2U6CiAgICAgICAgICAgICAgICBwcmludCgiSW5jb3JyZWN0IHBhc3N3b3JkLiIpCiAgICAgICAgZWxpZiBjaG9pY2UgPT0gIjIiOgogICAgICAgICAgICB1c2VyKG15Y3Vyc29yKQogICAgICAgIGVsaWYgY2hvaWNlID09ICIzIjoKICAgICAgICAgICAgcHJpbnQoIkV4aXRpbmcgdGhlIGFwcGxpY2F0aW9uLiIpCiAgICAgICAgICAgIGJyZWFrCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcHJpbnQoIkludmFsaWQgY2hvaWNlLiBUcnkgYWdhaW4uIikKCiMgUnVuIHRoZSBwcm9ncmFtCmlmIF9fbmFtZV9fID09ICJfX21haW5fXyI6CiAgICBjb25uZWN0aW9uID0gY29ubmVjdF90b19kYigpCiAgICBjdXJzb3IgPSBjb25uZWN0aW9uLmN1cnNvcigpCgogICAgc2V0dXBfZGF0YWJhc2UoY3Vyc29yKQogICAgaW5zZXJ0X3NhbXBsZV9kYXRhKGN1cnNvciwgY29ubmVjdGlvbikKICAgIG1haW5fbWVudShjdXJzb3IsIGNvbm5lY3Rpb24pCgogICAgY29ubmVjdGlvbi5jbG9zZSgpCg==
import mysql.connector
# Establish database connection
def connect_to_db():
try:
obj = mysql.connector.connect(
host="localhost",
user="root",
password="admin"
)
return obj
except mysql.connector.Error as err:
print(f"Error connecting to MySQL: {err}")
exit()
# Create database and tables
def setup_database(mycursor):
mycursor.execute("CREATE DATABASE IF NOT EXISTS airlines")
mycursor.execute("USE airlines")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS food_items (
sl_no INT AUTO_INCREMENT PRIMARY KEY,
food_name VARCHAR(40) NOT NULL,
price INT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS luggage (
luggage_id INT AUTO_INCREMENT PRIMARY KEY,
weight INT NOT NULL,
price INT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS cust_details (
cust_id INT AUTO_INCREMENT PRIMARY KEY,
cust_name VARCHAR(40) NOT NULL,
cont_no BIGINT NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS flight_details (
flight_id INT AUTO_INCREMENT PRIMARY KEY,
cus_id INT,
cus_name VARCHAR(40) NOT NULL,
departure VARCHAR(50),
destination VARCHAR(50),
flight_day DATE,
flight_time TIME,
FOREIGN KEY (cus_id) REFERENCES cust_details(cust_id)
)
""")
# Insert sample data into food_items
def insert_sample_data(mycursor, obj):
try:
sample_data = [
('Pepsi', 150),
('Coffee', 70),
('Tea', 50),
('Water', 60),
('Milk Shake', 80),
('Chicken Burger', 160),
('Cheese Pizza', 70),
('Chicken Biryani', 300),
('Plane Rice', 80),
('Aloo Paratha', 120),
('Roti Sabji', 100),
('Omelette', 50)
]
mycursor.executemany("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", sample_data)
obj.commit()
print("Sample data inserted successfully.")
except mysql.connector.Error as err:
print(f"Error inserting data: {err}")
# Admin functionalities
def admin(mycursor, obj):
while True:
print("\n--- Admin Menu ---")
print("1. View all data")
print("2. Manage food items")
print("3. Logout")
choice = input("Enter your choice: ")
if choice == "1":
mycursor.execute("SELECT * FROM food_items")
results = mycursor.fetchall()
for row in results:
print(row)
elif choice == "2":
manage_food_items(mycursor, obj)
elif choice == "3":
print("Logging out...")
break
else:
print("Invalid choice. Try again.")
# Manage food items
def manage_food_items(mycursor, obj):
while True:
print("\n--- Manage Food Items ---")
print("1. Add new food item")
print("2. Update food item price")
print("3. Delete food item")
print("4. Go back")
choice = input("Enter your choice: ")
if choice == "1":
food_name = input("Enter food name: ")
price = int(input("Enter food price: "))
try:
mycursor.execute("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", (food_name, price))
obj.commit()
print("Food item added successfully.")
except mysql.connector.Error as err:
print(f"Error: {err}")
elif choice == "2":
sl_no = int(input("Enter food item ID to update: "))
price = int(input("Enter new price: "))
try:
mycursor.execute("UPDATE food_items SET price = %s WHERE sl_no = %s", (price, sl_no))
obj.commit()
print("Price updated successfully.")
except mysql.connector.Error as err:
print(f"Error: {err}")
elif choice == "3":
sl_no = int(input("Enter food item ID to delete: "))
try:
mycursor.execute("DELETE FROM food_items WHERE sl_no = %s", (sl_no,))
obj.commit()
print("Food item deleted successfully.")
except mysql.connector.Error as err:
print(f"Error: {err}")
elif choice == "4":
break
else:
print("Invalid choice. Try again.")
# User functionalities
def user(mycursor):
while True:
print("\n--- User Menu ---")
print("1. View available food items")
print("2. Logout")
choice = input("Enter your choice: ")
if choice == "1":
mycursor.execute("SELECT * FROM food_items")
results = mycursor.fetchall()
print("\nAvailable Food Items:")
for row in results:
print(f"ID: {row[0]}, Name: {row[1]}, Price: {row[2]}")
elif choice == "2":
print("Logging out...")
break
else:
print("Invalid choice. Try again.")
# Main menu
def main_menu(mycursor, obj):
while True:
print("\n--- Main Menu ---")
print("1. Admin")
print("2. User")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == "1":
password = input("Enter admin password: ")
if password == "admin":
admin(mycursor, obj)
else:
print("Incorrect password.")
elif choice == "2":
user(mycursor)
elif choice == "3":
print("Exiting the application.")
break
else:
print("Invalid choice. Try again.")
# Run the program
if __name__ == "__main__":
connection = connect_to_db()
cursor = connection.cursor()
setup_database(cursor)
insert_sample_data(cursor, connection)
main_menu(cursor, connection)
connection.close()