-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathdatabases.py
71 lines (62 loc) · 1.6 KB
/
databases.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from model import Base, Student
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///students.db?check_same_thread=False')
Base.metadata.create_all(engine)
DBSession = sessionmaker(bind=engine)
session = DBSession()
def add_student(name, year, finished_lab=True):
"""
Add a student to the database, given
their name, year, and whether they have
finished the lab.
"""
student_object = Student(
name=name,
year=year,
finished_lab=finished_lab)
session.add(student_object)
session.commit()
def query_by_name(name):
"""
Find the first student in the database,
by their name
"""
student = session.query(Student).filter_by(
name=name).first()
return student
def query_all():
"""
Print all the students in the database.
"""
students = session.query(Student).all()
return students
def delete_student_id(id_number):
"""
Delete all students with a certain name
from the database.
"""
session.query(Student).filter_by(
student_id=id_number).delete()
session.commit()
def delete_student_name(name):
"""
Delete all students with a certain name
from the database.
"""
session.query(Student).filter_by(
name=name).delete()
session.commit()
def update_lab_status(name, finished_lab):
"""
Update a student in the database, with
whether or not they have finished the lab
"""
student_object = session.query(Student).filter_by(
name=name).first()
student_object.finished_lab = finished_lab
session.commit()
def query_by_id(student_id):
student = session.query(Student).filter_by(
student_id=student_id).first()
return student