Product Management System Cs Project
Product Management System Cs Project
connector
# Import MySQL connector for connecting Python with MySQL
from mysql.connector import Error
# Import error handling for MySQL
# Update a product
def update_product(connection, product_id, name=None, price=None, quantity=None):
try:
cursor = connection.cursor()
# Create a cursor object
updates = []
# List to hold parts of the update statement
params = []
# List to hold parameter values
# Delete a product
def delete_product(connection, product_id):
try:
cursor = connection.cursor()
# Create a cursor object
# SQL statement to delete a product by ID
sql = "DELETE FROM products WHERE id = %s"
cursor.execute(sql, (product_id,))
# Execute delete with parameter
connection.commit() # Commit changes
# Check if the product was deleted
if cursor.rowcount > 0:
print("Product deleted successfully.")
else:
print("Product not found.")
except Error as e:
# Handle errors during deletion
print(f"Error: {e}")
# Main application
def main():
# Establish database connection
connection = create_connection()
if connection is None:
print("Failed to connect to database. Exiting...")
return
create_table(connection)
# Create products table if not exists
if __name__ == "__main__":
main()
# Run the main function if the script is executed directly