import mysql.
connector
# Establish a connection to the database
def create_connection():
return [Link](
host="localhost",
user="your_username", # Replace with your MySQL username
password="your_password", # Replace with your MySQL password
database="food_portal_db" # Replace with your database name
# Function to add a new food item
def add_food_item(name, price, category, available=True):
conn = create_connection()
cursor = [Link]()
query = "INSERT INTO food_items (name, price, category, available) VALUES (%s, %s, %s, %s)"
values = (name, price, category, available)
[Link](query, values)
[Link]()
print("Food item added successfully!")
[Link]()
[Link]()
# Function to view all food items
def view_food_items():
conn = create_connection()
cursor = [Link]()
query = "SELECT * FROM food_items"
[Link](query)
results = [Link]()
print("Food Items:")
for row in results:
print(row)
[Link]()
[Link]()
# Function to update a food item's availability
def update_food_availability(food_id, available):
conn = create_connection()
cursor = [Link]()
query = "UPDATE food_items SET available = %s WHERE id = %s"
values = (available, food_id)
[Link](query, values)
[Link]()
print("Food item updated successfully!")
[Link]()
[Link]()
# Function to delete a food item
def delete_food_item(food_id):
conn = create_connection()
cursor = [Link]()
query = "DELETE FROM food_items WHERE id = %s"
values = (food_id,)
[Link](query, values)
[Link]()
print("Food item deleted successfully!")
[Link]()
[Link]()
# Sample usage of the functions
if __name__ == "__main__":
add_food_item("Pizza", 9.99, "Fast Food")
add_food_item("Burger", 5.49, "Fast Food")
view_food_items()
update_food_availability(1, False) # Update availability of item with ID 1
delete_food_item(2) # Delete item with ID 2
view_food_items()
Explanation of Functions
create_connection(): Establishes a connection to the MySQL database.
add_food_item(): Adds a new food item to the database with its name, price, category, and
availability.
view_food_items(): Fetches and displays all food items from the database.
update_food_availability(): Updates the availability of a food item based on its id.
delete_food_item(): Deletes a food item based on its id.