import mysql.
connector
# Database credentials
host = "localhost"
user = "your_username"
password = "your_password"
database = "your_database_name"
try:
# Connect to the database
conn = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
# Create a cursor object
cursor = conn.cursor()
# Function to get product details from the database
def get_product_details(batch_number):
sql = "SELECT product_name, price FROM products WHERE batch_number = %s"
cursor.execute(sql, (batch_number,))
result = cursor.fetchone()
if result:
return result[0], result[1]
else:
return None, None
# Function to generate and display the bill
def generate_bill(batch_number, quantity):
product_name, price = get_product_details(batch_number)
if product_name and price:
total_price = quantity * price
print("\n\n")
print("-" * 30)
print("Shopping Bill")
print("-" * 30)
print(f"Batch Number: {batch_number}")
print(f"Product Name: {product_name}")
print(f"Quantity: {quantity}")
print(f"Price per Unit: {price}")
print(f"Total Price: {total_price}")
print("-" * 30)
else:
print(f"Invalid Batch Number: {batch_number}")
# Get user input
batch_number = input("Enter Batch Number: ")
quantity = int(input("Enter Quantity: "))
# Generate and display the bill
generate_bill(batch_number, quantity)
except mysql.connector.Error as error:
print("Error while connecting to MySQL:", error)
finally:
if conn.is_connected():
cursor.close()
conn.close()
print("MySQL connection is closed")
CREATE TABLE products (
batch_number INT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
description TEXT,
category VARCHAR(50),
supplier_id INT
);
-- Add a foreign key constraint to the 'supplier_id' column (assuming you have a 'suppliers' table)
ALTER TABLE products
ADD CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id) REFERENCES suppliers(supplier_id);
-- Insert sample data with additional columns
INSERT INTO products (batch_number, product_name, price, description, category, supplier_id)
VALUES
(1, 'Product A', 10.50, 'High-quality product A description', 'Electronics', 1),
(2, 'Product B', 15.25, 'Product B description', 'Clothing', 2),
(3, 'Product C', 8.75, 'Product C description', 'Groceries', 3);
-- Create a 'suppliers' table (if not already created)
CREATE TABLE suppliers (
supplier_id INT PRIMARY KEY,
supplier_name VARCHAR(255) NOT NULL,
contact_info TEXT
);
-- Insert sample data into the 'suppliers' table
INSERT INTO suppliers (supplier_id, supplier_name, contact_info) VALUES
(1, 'Supplier A', 'Contact details for Supplier A'),
(2, 'Supplier B', 'Contact details for Supplier B'),
(3, 'Supplier C', 'Contact details for Supplier C');