Python Project – Patient Record System

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

SQL Queries

CREATE DATABASE hospital_db;
USE hospital_db;

CREATE TABLE patients (
    patient_id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    age INT,
    gender VARCHAR(10),
    contact VARCHAR(15)
);

CREATE TABLE visits (
    visit_id INT AUTO_INCREMENT PRIMARY KEY,
    patient_id INT,
    visit_date DATE,
    reason VARCHAR(255),
    diagnosis VARCHAR(255),
    treatment TEXT,
    FOREIGN KEY (patient_id) REFERENCES patients(patient_id)
);

CREATE TABLE payments (
    payment_id INT AUTO_INCREMENT PRIMARY KEY,
    visit_id INT,
    amount DECIMAL(10,2),
    method VARCHAR(50),
    payment_date DATE,
    FOREIGN KEY (visit_id) REFERENCES visits(visit_id)
);




INSERT INTO visits (patient_id, visit_date, reason, diagnosis, treatment) VALUES
(1, '2025-04-10', 'Fever and cough', 'Viral Infection', 'Rest, fluids, and paracetamol'),
(2, '2025-04-11', 'Stomach pain', 'Gastritis', 'Antacids and light diet'),
(3, '2025-04-12', 'Headache', 'Migraine', 'Pain relievers and stress management'),
(1, '2025-04-14', 'Follow-up check', 'Recovering', 'Continue medications'),
(2, '2025-04-15', 'Cold and sneezing', 'Allergic Rhinitis', 'Antihistamines'),
(3, '2025-04-16', 'Back pain', 'Muscle strain', 'Physiotherapy and painkillers');

Program 1

# Patient Record System 

import mysql.connector
from datetime import date

connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="root",
    database="hospital_db"
)
cursor = connection.cursor()

def add_patient():
    name = input("Name: ")
    age = int(input("Age: "))
    gender = input("Gender: ")
    contact = input("Contact: ")
    cursor.execute("INSERT INTO patients (name, age, gender, contact) VALUES (%s, %s, %s, %s)", 
                   (name, age, gender, contact))
    connection.commit()
    print("Patient added successfully.")

def record_visit():
    patient_id = int(input("Enter Patient ID: "))
    reason = input("Reason for Visit: ")
    diagnosis = input("Diagnosis: ")
    treatment = input("Treatment: ")
    cursor.execute("INSERT INTO visits (patient_id, visit_date, reason, diagnosis, treatment) VALUES (%s, %s, %s, %s, %s)",
                   (patient_id, date.today(), reason, diagnosis, treatment))
    visit_id = cursor.lastrowid
    amount = float(input("Payment Amount: "))
    method = input("Payment Method:(Cash/ Card/ UPI) ")
    cursor.execute("INSERT INTO payments (visit_id, amount, method, payment_date) VALUES (%s, %s, %s, %s)",
                   (visit_id, amount, method, date.today()))
    connection.commit()
    print("Visit and payment recorded.")

def show_patients():
    cursor.execute("SELECT * FROM patients")
    for row in cursor.fetchall():
        print(row)

def show_visits():
    cursor.execute("SELECT * FROM visits")
    for row in cursor.fetchall():
        print(row)

def main():
   
    while True:
        print("******* Patient Record System********")
        print("-------------------------------------------------")
        print("\n\t1. Add Patient")
        print("\t2. Record Visit")
        print("\t3. Show Patients")
        print("\t4. Show Visits")
        print("\t5. Exit")
        print("-------------------------------------------------")
        choice = input("Choice: ")
        if choice == '1':
            add_patient()
        elif choice == '2':
            record_visit()
        elif choice == '3':
            show_patients()
        elif choice == '4':
            show_visits()
        elif choice == '5':
            break
        else:
            print("Invalid choice plese enter between 1-5 only")


# Main Method calliing 
#if __name__ == "__main__":
main()

 

 

Did you like this article? If Yes, please give DataFlair 5 Stars on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply