0% found this document useful (0 votes)
4 views3 pages

computer program

The document provides a Python script that connects to a MySQL database to manage food items in a food portal. It includes functions to add, view, update availability, and delete food items from the database. Sample usage of these functions is also demonstrated.

Uploaded by

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

computer program

The document provides a Python script that connects to a MySQL database to manage food items in a food portal. It includes functions to add, view, update availability, and delete food items from the database. Sample usage of these functions is also demonstrated.

Uploaded by

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

import mysql.

connector

# Establish a connection to the database

def create_connection():

return mysql.connector.connect(

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 = conn.cursor()

query = "INSERT INTO food_items (name, price, category, available) VALUES (%s, %s, %s, %s)"

values = (name, price, category, available)

cursor.execute(query, values)

conn.commit()

print("Food item added successfully!")

cursor.close()

conn.close()

# Function to view all food items

def view_food_items():

conn = create_connection()

cursor = conn.cursor()

query = "SELECT * FROM food_items"

cursor.execute(query)

results = cursor.fetchall()

print("Food Items:")
for row in results:

print(row)

cursor.close()

conn.close()

# Function to update a food item's availability

def update_food_availability(food_id, available):

conn = create_connection()

cursor = conn.cursor()

query = "UPDATE food_items SET available = %s WHERE id = %s"

values = (available, food_id)

cursor.execute(query, values)

conn.commit()

print("Food item updated successfully!")

cursor.close()

conn.close()

# Function to delete a food item

def delete_food_item(food_id):

conn = create_connection()

cursor = conn.cursor()

query = "DELETE FROM food_items WHERE id = %s"

values = (food_id,)

cursor.execute(query, values)

conn.commit()

print("Food item deleted successfully!")

cursor.close()

conn.close()

# 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.

You might also like