fork download
  1. import mysql.connector
  2.  
  3. # Establish database connection
  4. def connect_to_db():
  5. try:
  6. obj = mysql.connector.connect(
  7. host="localhost",
  8. user="root",
  9. password="admin"
  10. )
  11. return obj
  12. except mysql.connector.Error as err:
  13. print(f"Error connecting to MySQL: {err}")
  14. exit()
  15.  
  16. # Create database and tables
  17. def setup_database(mycursor):
  18. mycursor.execute("CREATE DATABASE IF NOT EXISTS airlines")
  19. mycursor.execute("USE airlines")
  20.  
  21. mycursor.execute("""
  22. CREATE TABLE IF NOT EXISTS food_items (
  23. sl_no INT AUTO_INCREMENT PRIMARY KEY,
  24. food_name VARCHAR(40) NOT NULL,
  25. price INT NOT NULL
  26. )
  27. """)
  28.  
  29. mycursor.execute("""
  30. CREATE TABLE IF NOT EXISTS luggage (
  31. luggage_id INT AUTO_INCREMENT PRIMARY KEY,
  32. weight INT NOT NULL,
  33. price INT NOT NULL
  34. )
  35. """)
  36.  
  37. mycursor.execute("""
  38. CREATE TABLE IF NOT EXISTS cust_details (
  39. cust_id INT AUTO_INCREMENT PRIMARY KEY,
  40. cust_name VARCHAR(40) NOT NULL,
  41. cont_no BIGINT NOT NULL
  42. )
  43. """)
  44.  
  45. mycursor.execute("""
  46. CREATE TABLE IF NOT EXISTS flight_details (
  47. flight_id INT AUTO_INCREMENT PRIMARY KEY,
  48. cus_id INT,
  49. cus_name VARCHAR(40) NOT NULL,
  50. departure VARCHAR(50),
  51. destination VARCHAR(50),
  52. flight_day DATE,
  53. flight_time TIME,
  54. FOREIGN KEY (cus_id) REFERENCES cust_details(cust_id)
  55. )
  56. """)
  57.  
  58. # Insert sample data into food_items
  59. def insert_sample_data(mycursor, obj):
  60. try:
  61. sample_data = [
  62. ('Pepsi', 150),
  63. ('Coffee', 70),
  64. ('Tea', 50),
  65. ('Water', 60),
  66. ('Milk Shake', 80),
  67. ('Chicken Burger', 160),
  68. ('Cheese Pizza', 70),
  69. ('Chicken Biryani', 300),
  70. ('Plane Rice', 80),
  71. ('Aloo Paratha', 120),
  72. ('Roti Sabji', 100),
  73. ('Omelette', 50)
  74. ]
  75. mycursor.executemany("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", sample_data)
  76. obj.commit()
  77. print("Sample data inserted successfully.")
  78. except mysql.connector.Error as err:
  79. print(f"Error inserting data: {err}")
  80.  
  81. # Admin functionalities
  82. def admin(mycursor, obj):
  83. while True:
  84. print("\n--- Admin Menu ---")
  85. print("1. View all data")
  86. print("2. Manage food items")
  87. print("3. Logout")
  88.  
  89. choice = input("Enter your choice: ")
  90. if choice == "1":
  91. mycursor.execute("SELECT * FROM food_items")
  92. results = mycursor.fetchall()
  93. for row in results:
  94. print(row)
  95. elif choice == "2":
  96. manage_food_items(mycursor, obj)
  97. elif choice == "3":
  98. print("Logging out...")
  99. break
  100. else:
  101. print("Invalid choice. Try again.")
  102.  
  103. # Manage food items
  104. def manage_food_items(mycursor, obj):
  105. while True:
  106. print("\n--- Manage Food Items ---")
  107. print("1. Add new food item")
  108. print("2. Update food item price")
  109. print("3. Delete food item")
  110. print("4. Go back")
  111.  
  112. choice = input("Enter your choice: ")
  113. if choice == "1":
  114. food_name = input("Enter food name: ")
  115. price = int(input("Enter food price: "))
  116. try:
  117. mycursor.execute("INSERT INTO food_items (food_name, price) VALUES (%s, %s)", (food_name, price))
  118. obj.commit()
  119. print("Food item added successfully.")
  120. except mysql.connector.Error as err:
  121. print(f"Error: {err}")
  122. elif choice == "2":
  123. sl_no = int(input("Enter food item ID to update: "))
  124. price = int(input("Enter new price: "))
  125. try:
  126. mycursor.execute("UPDATE food_items SET price = %s WHERE sl_no = %s", (price, sl_no))
  127. obj.commit()
  128. print("Price updated successfully.")
  129. except mysql.connector.Error as err:
  130. print(f"Error: {err}")
  131. elif choice == "3":
  132. sl_no = int(input("Enter food item ID to delete: "))
  133. try:
  134. mycursor.execute("DELETE FROM food_items WHERE sl_no = %s", (sl_no,))
  135. obj.commit()
  136. print("Food item deleted successfully.")
  137. except mysql.connector.Error as err:
  138. print(f"Error: {err}")
  139. elif choice == "4":
  140. break
  141. else:
  142. print("Invalid choice. Try again.")
  143.  
  144. # User functionalities
  145. def user(mycursor):
  146. while True:
  147. print("\n--- User Menu ---")
  148. print("1. View available food items")
  149. print("2. Logout")
  150.  
  151. choice = input("Enter your choice: ")
  152. if choice == "1":
  153. mycursor.execute("SELECT * FROM food_items")
  154. results = mycursor.fetchall()
  155. print("\nAvailable Food Items:")
  156. for row in results:
  157. print(f"ID: {row[0]}, Name: {row[1]}, Price: {row[2]}")
  158. elif choice == "2":
  159. print("Logging out...")
  160. break
  161. else:
  162. print("Invalid choice. Try again.")
  163.  
  164. # Main menu
  165. def main_menu(mycursor, obj):
  166. while True:
  167. print("\n--- Main Menu ---")
  168. print("1. Admin")
  169. print("2. User")
  170. print("3. Exit")
  171.  
  172. choice = input("Enter your choice: ")
  173. if choice == "1":
  174. password = input("Enter admin password: ")
  175. if password == "admin":
  176. admin(mycursor, obj)
  177. else:
  178. print("Incorrect password.")
  179. elif choice == "2":
  180. user(mycursor)
  181. elif choice == "3":
  182. print("Exiting the application.")
  183. break
  184. else:
  185. print("Invalid choice. Try again.")
  186.  
  187. # Run the program
  188. if __name__ == "__main__":
  189. connection = connect_to_db()
  190. cursor = connection.cursor()
  191.  
  192. setup_database(cursor)
  193. insert_sample_data(cursor, connection)
  194. main_menu(cursor, connection)
  195.  
  196. connection.close()
  197.  
Success #stdin #stdout 0.02s 25796KB
stdin
Standard input is empty
stdout
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()