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

Text To PDF

Uploaded by

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

Text To PDF

Uploaded by

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

​import mysql.

connector

# Establishing a connection to the database


def connect_db():
return mysql.connector.connect(
host="localhost",
user="root",
password="password", # replace with your MySQL password
database="sweet_shop"
)

# Creating the 'sweets' table if it does not exist


def create_table():
db = connect_db()
cursor = db.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS sweets (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
quantity INT NOT NULL
)
""")
db.commit()
db.close()

# Adding a new sweet to the database


def add_sweet(name, price, quantity):
db = connect_db()
cursor = db.cursor()
cursor.execute("INSERT INTO sweets (name, price, quantity) VALUES (%s, %s, %s)", (name, price, quantity))
db.commit()
db.close()
print("Sweet added successfully!")

# Viewing all sweets in the shop


def view_sweets():
db = connect_db()
cursor = db.cursor()
cursor.execute("SELECT * FROM sweets")
sweets = cursor.fetchall()
db.close()
print("ID | Name | Price | Quantity")
for sweet in sweets:
print(sweet)
# Updating a sweet's information
def update_sweet(sweet_id, name=None, price=None, quantity=None):
db = connect_db()
cursor = db.cursor()
if name:
cursor.execute("UPDATE sweets SET name = %s WHERE id = %s", (name, sweet_id))
if price:
cursor.execute("UPDATE sweets SET price = %s WHERE id = %s", (price, sweet_id))
if quantity:
cursor.execute("UPDATE sweets SET quantity = %s WHERE id = %s", (quantity, sweet_id))
db.commit()
db.close()
print("Sweet updated successfully!")

# Deleting a sweet from the shop


def delete_sweet(sweet_id):
db = connect_db()
cursor = db.cursor()
cursor.execute("DELETE FROM sweets WHERE id = %s", (sweet_id,))
db.commit()
db.close()
print("Sweet deleted successfully!")

# Main menu for interacting with the program


def main_menu():
create_table() # Create the table if it doesn't exist
while True:
print("\nSweet Shop Management System")
print("1. Add Sweet")
print("2. View Sweets")
print("3. Update Sweet")
print("4. Delete Sweet")
print("5. Exit")
choice = input("Enter your choice: ")

if choice == '1':
name = input("Enter sweet name: ")
price = float(input("Enter sweet price: "))
quantity = int(input("Enter sweet quantity: "))
add_sweet(name, price, quantity)

elif choice == '2':


view_sweets()
elif choice == '3':
sweet_id = int(input("Enter sweet ID to update: "))
name = input("Enter new name (leave blank if no change): ") or None
price = input("Enter new price (leave blank if no change): ")
quantity = input("Enter new quantity (leave blank if no change): ")
update_sweet(sweet_id, name, float(price) if price else None, int(quantity) if quantity else None)

elif choice == '4':


sweet_id = int(input("Enter sweet ID to delete: "))
delete_sweet(sweet_id)

elif choice == '5':


print("Exiting... Thank you!")
break

else:
print("Invalid choice! Please try again.")

# Run the main menu function


if __name__ == "__main__":
main_menu()

You might also like