import mysql.
connector
from mysql.connector import Error
# Database configuration
config = {
'user': 'your_username',
'password': 'your_password',
'host': '127.0.0.1',
'database': 'bakery_db'
def create_connection():
"""Create and return a database connection."""
try:
connection = mysql.connector.connect(**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():
connection.close()
print("Connection closed")
def add_item(name, price, quantity):
"""Add a new item to the bakery."""
connection = create_connection()
if connection:
cursor = connection.cursor()
try:
cursor.execute("INSERT INTO items (name, price, quantity) VALUES (%s, %s, %s)", (name, price,
quantity))
connection.commit()
print("Item added successfully")
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)
def view_items():
"""View all items in the bakery."""
connection = create_connection()
if connection:
cursor = connection.cursor()
try:
cursor.execute("SELECT * FROM items")
rows = cursor.fetchall()
for row in rows:
print(row)
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
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 = connection.cursor()
try:
updates = []
if name:
updates.append("name = %s")
if price:
updates.append("price = %s")
if quantity:
updates.append("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]
cursor.execute(sql, params)
connection.commit()
print("Item updated successfully")
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)
def delete_item(item_id):
"""Delete an item from the bakery."""
connection = create_connection()
if connection:
cursor = connection.cursor()
try:
cursor.execute("DELETE FROM items WHERE id = %s", (item_id,))
connection.commit()
print("Item deleted successfully")
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
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.")