0% found this document useful (0 votes)
6 views5 pages

MySQL Inventory Management System

The document outlines a Python program for managing an inventory database using MySQL. It includes functionalities to add, view, update, delete, and search for products in the inventory. The program establishes a connection to the database and provides a user interface for inventory management operations.

Uploaded by

deepakb13102008
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)
6 views5 pages

MySQL Inventory Management System

The document outlines a Python program for managing an inventory database using MySQL. It includes functionalities to add, view, update, delete, and search for products in the inventory. The program establishes a connection to the database and provides a user interface for inventory management operations.

Uploaded by

deepakb13102008
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

CREATE DATABASE inventory_db;

USE inventory_db;

CREATE TABLE products


(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
category VARCHAR(50),
price DECIMAL(10, 2) NOT NULL,
stock_quantity INT NOT NULL
);
import [Link]
from [Link] import Error

class InventoryManager:
def __init__(self):
try:
[Link] = [Link](
host='localhost',
user='root', # Replace with your MySQL username
password='password', # Replace with your MySQL password
database='inventory_db'
)
[Link] = [Link](dictionary=True)
print("Successfully connected to the database.")
except Error as e:
print(f"Error connecting to MySQL: {e}")
exit()

def add_product(self):
print("\n--- Add New Product ---")
name = input("Enter product name: ")
category = input("Enter category: ")
try:
price = float(input("Enter price: "))
stock = int(input("Enter stock quantity: "))

query = "INSERT INTO products (name, category, price, stock_quantity) VALUES


(%s, %s, %s, %s)"
[Link](query, (name, category, price, stock))
[Link]()
print(f"Product '{name}' added successfully!")
except ValueError:
print("Invalid input. Price must be a decimal and stock an integer.")

def view_inventory(self):
print("\n--- Current Inventory ---")
[Link]("SELECT * FROM products")
products = [Link]()

if not products:
print("Inventory is empty.")
return

print(f"{'ID':<5} {'Name':<20} {'Category':<15} {'Price':<10} {'Stock':<5}")


print("-" * 55)
for p in products:
print(f"{p['id']:<5} {p['name']:<20} {p['category']:<15} {p['price']:<10}
{p['stock_quantity']:<5}")

def update_stock(self):
self.view_inventory()
try:
p_id = int(input("\nEnter Product ID to update: "))
new_stock = int(input("Enter new stock quantity: "))
query = "UPDATE products SET stock_quantity = %s WHERE id = %s"
[Link](query, (new_stock, p_id))
[Link]()

if [Link] > 0:
print("Stock updated successfully.")
else:
print("Product ID not found.")
except ValueError:
print("Please enter valid numeric IDs and stock levels.")

def delete_product(self):
self.view_inventory()
try:
p_id = int(input("\nEnter Product ID to delete: "))
confirm = input(f"Are you sure you want to delete ID {p_id}? (y/n): ")

if [Link]() == 'y':
query = "DELETE FROM products WHERE id = %s"
[Link](query, (p_id,))
[Link]()
print("Product removed.")
except ValueError:
print("Invalid ID.")

def search_product(self):
search_term = input("\nEnter product name to search: ")
query = "SELECT * FROM products WHERE name LIKE %s"
[Link](query, (f"%{search_term}%",))
results = [Link]()
if results:
for p in results:
print(f"ID: {p['id']} | Name: {p['name']} | Stock: {p['stock_quantity']}")
else:
print("No matching products found.")

def run(self):
while True:
print("\n==============================")
print(" INVENTORY MANAGEMENT SYSTEM")
print("==============================")
print("1. View Inventory")
print("2. Add Product")
print("3. Update Stock")
print("4. Delete Product")
print("5. Search Product")
print("6. Exit")

choice = input("\nSelect an option (1-6): ")

if choice == '1': self.view_inventory()


elif choice == '2': self.add_product()
elif choice == '3': self.update_stock()
elif choice == '4': self.delete_product()
elif choice == '5': self.search_product()
elif choice == '6':
print("Closing connection... Goodbye!")
[Link]()
[Link]()
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
app = InventoryManager()
[Link]()
OUTPUT

You might also like