bakery working source code
bakery working source code
connector
# Database configuration
config = {
'user': 'your_username',
'password': 'your_password',
'host': '127.0.0.1',
'database': 'bakery_db'
def create_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):
if connection.is_connected():
connection.close()
print("Connection closed")
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()
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)
def view_items():
connection = create_connection()
if connection:
cursor = connection.cursor()
try:
rows = cursor.fetchall()
print(row)
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(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")
params = [param for param in (name, price, quantity) if param is not None] + [item_id]
cursor.execute(sql, params)
connection.commit()
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)
def delete_item(item_id):
connection = create_connection()
if connection:
cursor = connection.cursor()
try:
cursor.execute("DELETE FROM items WHERE id = %s", (item_id,))
connection.commit()
except Error as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)
# Example Usage
if __name__ == "__main__":
while True:
print("5. Exit")
if choice == '1':
view_items()
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): ")
delete_item(item_id)
break
else: