from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017/")
db = client["StudentDatabase"]
collection = db["Students"]
def add_student():
student_id =
input("Enter Student ID: ")
student_name =
input("Enter Student Name: ")
course = input("Enter
Course: ")
student_data = {
"Student_id": student_id,
"Student_name": student_name,
"Course":
course
}
result =
collection.insert_one(student_data)
print("Student added
with ID:", result.inserted_id)
def display_students():
students =
collection.find()
for student in students:
print("Student
ID:", student["Student_id"])
print("Student
Name:", student["Student_name"])
print("Course:", student["Course"])
print("-" *
20)
def update_student():
student_id =
input("Enter Student ID to update: ")
new_course =
input("Enter new Course: ")
result =
collection.update_one({"Student_id": student_id}, {"$set":
{"Course": new_course}})
if result.modified_count
> 0:
print("Student
record updated successfully")
else:
print("No student
found with the given ID")
def delete_student():
student_id =
input("Enter Student ID to delete: ")
result =
collection.delete_one({"Student_id": student_id})
if result.deleted_count
> 0:
print("Student
record deleted successfully")
else:
print("No student
found with the given ID")
while True:
print("\nMenu:")
print("1. Add
Student")
print("2. Display Students")
print("3. Update
Student")
print("4. Delete
Student")
print("5. Exit")
choice = input("Enter
your choice: ")
if choice ==
"1":
add_student()
elif choice ==
"2":
display_students()
elif choice ==
"3":
update_student()
elif choice ==
"4":
delete_student()
elif choice ==
"5":
print("Exiting
program")
break
else:
print("Invalid
choice. Please select again.")
No comments:
Post a Comment