To achieve this functionality, you can create a simple program in Python that allows users to enter personal details, save these details to a text file, and perform operations like displaying all records, searching for specific records, and deleting records based on specific criteria. Below is a sample program that demonstrates these capabilities:
import os
# Define the file to store the database
DB_FILE = "database.txt"
def save_record(record):
with open(DB_FILE, "a") as file:
file.write(f"{record}\n")
def load_records():
if not os.path.exists(DB_FILE):
return []
with open(DB_FILE, "r") as file:
return [line.strip() for line in file.readlines()]
def display_records(records):
for idx, record in enumerate(records, start=1):
print(f"{idx}: {record}")
def search_records(records, search_term):
return [record for record in records if search_term.lower() in record.lower()]
def delete_record(records, search_term):
return [record for record in records if search_term.lower() not in record.lower()]
def main():
while True:
print("1. Add Record")
print("2. Display Records")
print("3. Search Records")
print("4. Delete Record")
print("5. Exit")
choice = input("Enter your choice: ")
if choice == "1":
father_name = input("Enter father's name: ")
national_number = input("Enter national number: ")
dob = input("Enter date of birth (DD-MM-YYYY): ")
educational_qualification = input("Enter educational qualification: ")
telephone_number = input("Enter telephone number: ")
position = input("Enter position: ")
address = input("Enter address: ")
record = f"{father_name},{national_number},{dob},{educational_qualification},{telephone_number},{position},{address}"
save_record(record)
print("Record saved successfully.\n")
elif choice == "2":
records = load_records()
if records:
print("Displaying all records:")
display_records(records)
else:
print("No records found.\n")
elif choice == "3":
search_term = input("Enter search term: ")
records = load_records()
if records:
search_results = search_records(records, search_term)
if search_results:
print("Search Results:")
display_records(search_results)
else:
print("No records found for the search term.\n")
else:
print("No records found.\n")
elif choice == "4":
search_term = input("Enter term for deletion: ")
records = load_records()
if records:
new_records = delete_record(records, search_term)
with open(DB_FILE, "w") as file:
for record in new_records:
file.write(f"{record}\n")
print("Record(s) deleted successfully.\n")
else:
print("No records found.\n")
elif choice == "5":
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.\n")
if __name__ == "__main__":
main()
Explanation:
-
save_record(record): This function appends a new record to the database file.
-
load_records(): This function reads all lines from the database file and returns them as a list of records.
-
display_records(records): This function prints all records stored in the database.
-
search_records(records, search_term): This function returns a list of records that contain the search term.
-
delete_record(records, search_term): This function removes records that match the search term and returns the new list of records.
-
main(): This function implements a simple command-line interface to interact with the database, allowing the user to add new records, display them, search through them, delete specific records, or exit the application.
This basic framework can be expanded or modified to fit more specific needs, such as formatting outputs, enhancing search functionality, or handling different data storage mechanisms.