Grocery_Management_System_using_Python[1]
Grocery_Management_System_using_Python[1]
inventory management, track customer purchases, and generate reports. Below is a step-by-
step guide to building such a system.
sql
Copy code
CREATE DATABASE grocery_management;
USE grocery_management;
bash
Copy code
pip install mysql-connector pandas
3. Python Script:
o The following Python script handles MySQL connectivity, CSV file
reading/writing, and managing products and customer orders.
Python Code
python
Copy code
import mysql.connector
import pandas as pd
from datetime import datetime
# MySQL Connection
def connect_to_database():
try:
conn = mysql.connector.connect(
host="localhost", # MySQL host
user="root", # MySQL user
password="password", # MySQL password
database="grocery_management" # Database name
)
if conn.is_connected():
print("Successfully connected to the database.")
return conn
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
cursor.execute("""
CREATE TABLE IF NOT EXISTS customer_orders (
id INT AUTO_INCREMENT PRIMARY KEY,
customer_name VARCHAR(255),
product_id INT,
quantity INT,
order_time DATETIME,
payment_status VARCHAR(50),
total_amount DECIMAL(10, 2),
FOREIGN KEY (product_id) REFERENCES products(id)
);
""")
conn.commit()
print("Sample data inserted.")
cursor.close()
conn.close()
# Fetch products
cursor.execute("SELECT * FROM products;")
products = cursor.fetchall()
print("\nProducts:")
for product in products:
print(product)
print("\nCustomer Orders:")
for order in orders:
print(order)
cursor.close()
conn.close()
cursor.close()
conn.close()
conn.commit()
print("Data imported from CSV to MySQL.")
cursor.close()
conn.close()
except FileNotFoundError:
print("CSV file not found.")
except Exception as e:
print(f"Error: {e}")
cursor.close()
conn.close()
if payment_status == "Paid":
print(f"Order {order_id} marked as paid.")
else:
print(f"Payment for order {order_id} is pending.")
cursor.close()
conn.close()
if choice == '1':
customer_name = input("Enter customer name: ")
product_name = input("Enter product name: ")
quantity = int(input("Enter quantity: "))
place_order(customer_name, product_name, quantity)
elif choice == '2':
order_id = int(input("Enter order ID: "))
payment_status = input("Enter payment status (Paid/Pending): ")
update_payment_status(order_id, payment_status)
elif choice == '3':
fetch_data_from_db()
elif choice == '4':
export_to_csv()
elif choice == '5':
import_from_csv()
elif choice == '6':
create_and_insert_sample_data()
elif choice == '7':
break
else:
print("Invalid choice! Please try again.")
if __name__ == "__main__":
main()
Explanation:
1. Database Setup:
o The script creates a grocery_management database with products and
customer_orders tables.
o products stores information about grocery items such as name, category,
price, and stock quantity.
o customer_orders stores customer orders, including the customer’s name,
product ID, quantity, order time, payment status, and total amount.
2. Features:
o Place an Order: Customers can place orders for grocery items. The system
checks for stock availability and updates the stock quantity accordingly.
o Update Payment Status: The system allows updating the payment status for
orders (e.g., "Paid" or "Pending").
o Export/Import Data: The system can export product and order data to CSV
files and import data from CSV files.
o Sample Data: The script can insert sample product data into the database for
testing purposes.
3. CSV Handling:
o The system supports exporting and importing product and order data to/from
CSV files using pandas.
Example Output:
1. Place an Order:
mathematica
Copy code
Order placed for John Doe: 3 x Milk - Total: 4.50
2. Export Data:
kotlin
Copy code
Products data exported to 'products.csv'.
Orders data exported to 'orders.csv'.
css
Copy code
Products:
{'id': 1, 'name': 'Milk', 'category': 'Dairy', 'price': 1.5,
'stock_quantity': 100}
Customer Orders:
{'id': 1, 'customer_name': 'John Doe', 'product_id': 1, 'quantity':
3, 'order_time': '2024-11-09 10:00:00', 'payment_status': 'Pending',
'total_amount': 4.50}
How to Test:
This grocery management system can effectively handle inventory and customer orders, and
allows easy integration with CSV files for reporting and backups.