MyCSProject

By: Anonymous12/22/202526 views Public Note
import mysql.connector from mysql.connector import Error #DATABASE CONNECTION ---------- def create_connection(): try: conn = mysql.connector.connect( host="localhost", # Change if needed user="root", # Your MySQL username password="root", # Your MySQL password database="billing_db" # Database name ) return conn except Error as e: print(f"Error: {e}") return None # ---------- INITIAL SETUP ---------- def setup_database(): conn = create_connection() if conn: cursor = conn.cursor() # Create table if not exists cursor.execute(""" CREATE TABLE IF NOT EXISTS products ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, price FLOAT NOT NULL ) """) cursor.execute(""" CREATE TABLE IF NOT EXISTS bills ( bill_id INT AUTO_INCREMENT PRIMARY KEY, product_name VARCHAR(50), quantity INT, total_price FLOAT ) """) conn.commit() conn.close() # ---------- ADD PRODUCT ---------- def add_product(name, price): conn = create_connection() if conn: cursor = conn.cursor() cursor.execute("INSERT INTO products (name, price) VALUES (%s, %s)", (name, price)) conn.commit() conn.close() print("✅ Product added successfully!") # ---------- VIEW PRODUCTS ---------- def view_products(): conn = create_connection() if conn: cursor = conn.cursor() cursor.execute("SELECT * FROM products") rows = cursor.fetchall() print("\n--- Product List ---") for row in rows: print(f"ID: {row[0]} | Name: {row[1]} | Price: ₹{row[2]}") conn.close() # ---------- GENERATE BILL ---------- def generate_bill(product_id, quantity): conn = create_connection() if conn: cursor = conn.cursor() cursor.execute("SELECT name, price FROM products WHERE id=%s", (product_id,)) product = cursor.fetchone() if product: total_price = product[1] * quantity cursor.execute("INSERT INTO bills (product_name, quantity, total_price) VALUES (%s, %s, %s)", (product[0], quantity, total_price)) conn.commit() print(f"? Bill Generated: {product[0]} x {quantity} = ₹{total_price}") else: print("❌ Product not found!") conn.close() # ---------- VIEW ALL BILLS ---------- def view_bills(): conn = create_connection() if conn: cursor = conn.cursor() cursor.execute("SELECT * FROM bills") rows = cursor.fetchall() print("\n--- All Bills ---") for row in rows: print(f"Bill ID: {row[0]} | Product: {row[1]} | Qty: {row[2]} | Total: ₹{row[3]}") conn.close() # ---------- MAIN MENU ---------- def main(): setup_database() while True: print("\n===== Billing System Menu =====") print("1. Add Product") print("2. View Products") print("3. Generate Bill") print("4. View All Bills") print("5. Exit") choice = input("Enter choice: ") if choice == "1": name = input("Enter product name: ") try: price = float(input("Enter product price: ")) add_product(name, price) except ValueError: print("❌ Invalid price!") elif choice == "2": view_products() elif choice == "3": try: product_id = int(input("Enter product ID: ")) quantity = int(input("Enter quantity: ")) generate_bill(product_id, quantity) except ValueError: print("❌ Invalid input!") elif choice == "4": view_bills() elif choice == "5": print("Exiting... ✅") break else: print("❌ Invalid choice!") if __name__ == "__main__": main()

Want to create your own notes?

Join thousands of users writing securely on ProNotepad.