import mysql.
connector
from [Link] import Error
# Database configuration
config = {
'user': 'your_username',
'password': 'your_password',
'host': '[Link]',
'database': 'bakery_db'
def create_connection():
"""Create and return a database connection."""
try:
connection = [Link](**config)
if connection.is_connected():
print("Connection established")
return connection
except Error as e:
print(f"Error: {e}")
return None
def close_connection(connection):
"""Close the database connection."""
if connection.is_connected():
[Link]()
print("Connection closed")
def add_item(name, price, quantity):
"""Add a new item to the bakery."""
connection = create_connection()
if connection:
cursor = [Link]()
try:
[Link]("INSERT INTO items (name, price, quantity) VALUES (%s, %s, %s)", (name, price,
quantity))
[Link]()
print("Item added successfully")
except Error as e:
print(f"Error: {e}")
finally:
[Link]()
close_connection(connection)
def view_items():
"""View all items in the bakery."""
connection = create_connection()
if connection:
cursor = [Link]()
try:
[Link]("SELECT * FROM items")
rows = [Link]()
for row in rows:
print(row)
except Error as e:
print(f"Error: {e}")
finally:
[Link]()
close_connection(connection)
def update_item(item_id, name=None, price=None, quantity=None):
"""Update an existing item in the bakery."""
connection = create_connection()
if connection:
cursor = [Link]()
try:
updates = []
if name:
[Link]("name = %s")
if price:
[Link]("price = %s")
if quantity:
[Link]("quantity = %s")
update_str = ", ".join(updates)
sql = f"UPDATE items SET {update_str} WHERE id = %s"
params = [param for param in (name, price, quantity) if param is not None] + [item_id]
[Link](sql, params)
[Link]()
print("Item updated successfully")
except Error as e:
print(f"Error: {e}")
finally:
[Link]()
close_connection(connection)
def delete_item(item_id):
"""Delete an item from the bakery."""
connection = create_connection()
if connection:
cursor = [Link]()
try:
[Link]("DELETE FROM items WHERE id = %s", (item_id,))
[Link]()
print("Item deleted successfully")
except Error as e:
print(f"Error: {e}")
finally:
[Link]()
close_connection(connection)
# Example Usage
if __name__ == "__main__":
while True:
print("\nBakery Management System")
print("1. Add Item")
print("2. View Items")
print("3. Update Item")
print("4. Delete Item")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
name = input("Enter item name: ")
price = float(input("Enter item price: "))
quantity = int(input("Enter item quantity: "))
add_item(name, price, quantity)
elif choice == '2':
view_items()
elif choice == '3':
item_id = int(input("Enter item ID to update: "))
name = input("Enter new item name (leave blank to keep current): ")
price = input("Enter new item price (leave blank to keep current): ")
quantity = input("Enter new item quantity (leave blank to keep current): ")
price = float(price) if price else None
quantity = int(quantity) if quantity else None
update_item(item_id, name or None, price, quantity)
elif choice == '4':
item_id = int(input("Enter item ID to delete: "))
delete_item(item_id)
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")