Garbage Python

Garbage
mail@pastecode.io avatar
unknown
python
3 years ago
4.2 kB
0
Indexable
Never
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

# get baseclass from table descriptions
Base = declarative_base()


def menu():
    print("1. Read all students\n"
          "2. Get student by id\n"
          "3. Add Student\n"
          "4. Edit Student\n"
          "5. Remove student\n"
          "6. Search\n"
          "7. Exit")


# Describing out table
class Students(Base):
    __tablename__ = 'students'

    id = Column(Integer, primary_key=True)
    name = Column(String)
    email = Column(String, unique=True)
    year = Column(Integer)


def main():
    # engine describes the database url
    engine = create_engine('sqlite:///school.sqlite')

    # Create a session maker (factory pattern)
    Session = sessionmaker(bind=engine)

    # Create a session using session maker
    session = Session()

    students = session.query(Students).all()

    loop_number = 0

    while loop_number != 7:
        menu()
        user_input = input()

        if user_input == '1':

            if len(students) == 0:
                print("No students found")
            else:
                session.refresh(students)
                for student in students:
                    print(f'id: {student.id}, name: {student.name}, email: {student.email}, year: {student.year}')

        if user_input == '2':
            chosen_id = int(input('Insert the id of the student you want to find'))
            chosen_query = session.query(Students).filter(Students.id == chosen_id).all()
            if session.query(Students).filter(Students.id == chosen_id).first() is None:
                print("Student not found")
            else:
                print(f'name: {chosen_query[0].name}, email: {chosen_query[0].email}, year: {chosen_query[0].year}')

        if user_input == '3':
            in_name = input("Insert name of student:")
            in_Email = input("Insert email of student:")
            in_Year = int(input("insert birthyear of student:"))

            new_student = Students(name=in_name, email=in_Email, year=in_Year)
            session.add(new_student)
            session.commit()
            print(f'Added student: id: {new_student.id}, name: {in_name}, email: {in_Email}, year: {in_Year}')

        if user_input == '4':
            edit_id = int(input("input the id of the student you want to edit:"))
            edit_name = input("input the name of the student you want to edit")
            edit_email = input("email of the student you want to edit")
            edit_year = input("Year born of the student you want to edit")

            if session.query(Students).filter(Students.id == edit_id).first() is None:
                print("Student not found")
            else:
                x = session.query(Students).get(edit_id)
                x.name = edit_name
                x.email = edit_email
                x.year = edit_year
                session.commit()
                print("Student was edited successfully")

        if user_input == '5':
            delete_input = int(input("Select the id of the student you want to delete:"))
            if session.query(Students).filter(Students.id == delete_input).first() is None:
                print("Student not found")
            else:
                session.query(Students).filter(Students.id == delete_input).delete()
                session.commit()
                print("Student was removed successfully")

        if user_input == '6':
            user_search = input('write down the name of the student you want to search for')
            search_result = session.query(Students).filter(Students.name.like(user_search))
            if search_result.first() is None:
                print("No students found")
            else:
                for each_student in search_result:
                    print(f'id: {each_student.id}, name: {each_student.name}, email: {each_student.email}, year: {each_student.year}')

        if user_input == '7':
            loop_number = 7


if __name__ == '__main__':
    main()