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

Python CODE

This document contains a Python script for a Pharmacy Management System that allows users to add, view, delete, and update medications in a MySQL database. It also includes functionality to check stock availability and generate bills for purchases. The script features a command-line interface for user interaction.

Uploaded by

shamikshajanagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Python CODE

This document contains a Python script for a Pharmacy Management System that allows users to add, view, delete, and update medications in a MySQL database. It also includes functionality to check stock availability and generate bills for purchases. The script features a command-line interface for user interaction.

Uploaded by

shamikshajanagan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

import mysql.

connector

def create_connection():

connection = mysql.connector.connect(

host='localhost',

user='root',

password='password',

database='pharmacy_db')

return connection

def add_medication(name, quantity, price):

connection = create_connection()

cursor = connection.cursor()

cursor.execute("INSERT INTO medications (name, quantity, price) VALUES (%s, %s, %s)", (name,
quantity, price))

connection.commit()

cursor.close()

connection.close()

print_box(f"Medication '{name}' added successfully.")

def view_medications():

connection = create_connection()

cursor = connection.cursor()

cursor.execute("SELECT * FROM medications")

rows = cursor.fetchall()

cursor.close()

connection.close()

print("Medications in the database:")


for row in rows:

print_box(f"ID: {row[0]}, Name: {row[1]}, Quantity: {row[2]}, Price: {row[3]}")

# Function to delete medication

def delete_medication(medication_id):

connection = create_connection()

cursor = connection.cursor()

cursor.execute("DELETE FROM medications WHERE id = %s", (medication_id,))

connection.commit()

cursor.close()

connection.close()

print(f"Medication with ID {medication_id} deleted successfully.")

def update_stock(medication_id, quantity):

connection = create_connection()

cursor = connection.cursor()

cursor.execute("UPDATE medications SET quantity = quantity + %s WHERE id = %s", (quantity,


medication_id))

connection.commit()

cursor.close()

connection.close()

print_box(f"Stock updated for medication ID {medication_id}. New quantity: {quantity}.")

def view_stock(medication_id):

connection = create_connection()

cursor = connection.cursor()

cursor.execute("SELECT quantity FROM medications WHERE id = %s", (medication_id,))

result = cursor.fetchone()

cursor.close()
connection.close()

if result:

print_box(f"Current stock for medication ID {medication_id}: {result[0]}")

else:

print_box(f"No medication found with ID {medication_id}.")

def generate_bill(medication_id, quantity):

connection = create_connection()

cursor = connection.cursor()

cursor.execute("SELECT name, price, quantity FROM medications WHERE id = %s", (medication_id,))

result = cursor.fetchone()

cursor.close()

connection.close()

if result:

name, price, available_quantity = result

if quantity > available_quantity:

print_box(f"Insufficient stock. Available quantity for '{name}': {available_quantity}.")

elif quantity <= 0:

print_box("Quantity must be greater than zero.")

else:

total_cost = price * quantity

output = (

SSf"\n--- Bill ---\n"

f"Medication: {name}\n"

f"Quantity Bought: {quantity}\n"

f"Price per Unit: {price:.2f}\n"

f"Total Cost: {total_cost:.2f}\n"


f"Thank you for your purchase!")

print_box(output)

else:

print_box(f"No medication found with ID {medication_id}.")

def print_box(message):

border = '+' + '-' * (len(message) + 2) + '+'

print(border)

print(f"| {message} |")

print(border)

def main():

while True:

print("\nPharmacy Management System")

print("1. Add Medication")

print("2. View Medications")

print("3. Delete Medication")

print("4. Update Stock")

print("5. View Stock Availability")

print("6. Generate Bill")

print("7. Exit")

choice = input("Enter your choice: ")

if choice == '1':

name = input("Enter medication name: ")

quantity = int(input("Enter quantity: "))

price = float(input("Enter price: "))


add_medication(name, quantity, price)

elif choice == '2':

view_medications()

elif choice == '3':

medication_id = int(input("Enter medication ID to delete: "))

delete_medication(medication_id)

elif choice == '4':

medication_id = int(input("Enter medication ID to update stock: "))

quantity = int(input("Enter quantity to add (use negative number to decrease): "))

update_stock(medication_id, quantity)

elif choice == '5':

medication_id = int(input("Enter medication ID to view stock: "))

view_stock(medication_id)

elif choice == '6':

medication_id = int(input("Enter medication ID to generate bill: "))

quantity = int(input("Enter quantity to purchase: "))

generate_bill(medication_id, quantity)

elif choice == '7':

print("Exiting the system.")

break

else:

print("Invalid choice. Please try again.")

if __name__ == "__main__":

main()

You might also like