0% found this document useful (0 votes)
111 views4 pages

SOURCE CODE

souce code

Uploaded by

chethanraajasn
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)
111 views4 pages

SOURCE CODE

souce code

Uploaded by

chethanraajasn
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/ 4

import mysql.

connector

# Connect to the database


def connect_db():
return mysql.connector.connect(
host="localhost",
user="root", # Replace with your MySQL username
password="22112007", # Replace with your MySQL
password
database="hospital_management"
)

# Function to add a patient


def add_patient(connection):
cursor = connection.cursor()
name = input("Enter patient name: ")
age = int(input("Enter patient age: "))
contact = input("Enter contact number: ")
query = "INSERT INTO patients (name, age, contact)
VALUES (%s, %s, %s)"
cursor.execute(query, (name, age, contact))
connection.commit()
print("Patient added successfully.\n")

# Function to view all patients


def view_patients(connection):
cursor = connection.cursor()
cursor.execute("SELECT * FROM patients")
for patient in cursor.fetchall():
print(f"ID: {patient[0]}, Name: {patient[1]}, Age:
{patient[2]}, Contact: {patient[3]}")
print()

# Function to add a doctor


def add_doctor(connection):
cursor = connection.cursor()
name = input("Enter doctor name: ")
specialization = input("Enter specialization: ")
query = "INSERT INTO doctors (name, specialization)
VALUES (%s, %s)"
cursor.execute(query, (name, specialization))
connection.commit()
print("Doctor added successfully.\n")

# Function to view all doctors


def view_doctors(connection):
cursor = connection.cursor()
cursor.execute("SELECT * FROM doctors")
for doctor in cursor.fetchall():
print(f"ID: {doctor[0]}, Name: {doctor[1]}, Specialization:
{doctor[2]}")
print()

# Main menu
def main():
connection = connect_db()
while True:
print("\n1. Add Patient\n2. View Patients\n3. Add
Doctor\n4. View Doctors\n5. Exit")
choice = input("Enter your choice: ")
if choice == '1':
add_patient(connection)
elif choice == '2':
view_patients(connection)
elif choice == '3':
add_doctor(connection)
elif choice == '4':
view_doctors(connection)
elif choice == '5':
connection.close()
print("Exiting...")
break
else:
print("Invalid choice. Please try again.")

if __name__ == "__main__":
main()

You might also like