-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_crud.py
131 lines (103 loc) · 3.13 KB
/
old_crud.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import sqlite3, datetime, time
connection = sqlite3.connect("register.db")
run_cursor = connection.cursor()
class Student(object):
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
def save_student(self):
add_student_query = '''
INSERT INTO students (first_name, last_name)
VALUES ('{0}', '{1}')
'''.format(self.firstname, self.lastname)
if run_cursor.execute(add_student_query):
connection.commit()
return "Student\n" + str(self) + " saved!"
return "Error adding student!"
@staticmethod
def delete_student(student_id):
delete_student_query = '''
DELETE FROM students WHERE
student_id = {0}
'''.format(student_id)
if run_cursor.execute(delete_student_query):
connection.commit()
return "Student " + str(student_id) + " deleted!"
return "Error deleting student ID: " + str(student_id)
@staticmethod
def get_all_students():
get_all_students_query = '''
SELECT * FROM students
'''
if run_cursor.execute(get_all_students_query):
students = run_cursor.fetchall()
return students
@staticmethod
def get_student_details(student_id):
get_student_details_query = '''
SELECT first_name, last_name
FROM students WHERE student_id = {0}
'''.format(student_id)
if run_cursor.execute(get_student_details_query):
student_details = run_cursor.fetchall()
return student_details
@staticmethod
def get_all_student_ids():
get_all_student_ids_query = '''
SELECT student_id FROM students
'''
if run_cursor.execute(get_all_student_ids_query):
student_ids = [row[0] for row in run_cursor.fetchall()]
return student_ids
def __str__(self):
return "First Name: " + self.firstname \
+ ", Last Name: " + str(self.lastname)
class Classes(object):
def __init__(self, subject):
self.subject = subject
def save_class(self):
subject = self.subject
add_subject_query = '''
INSERT INTO classes (subject)
VALUES ('{0}')
'''.format(self.subject)
if run_cursor.execute(add_subject_query):
connection.commit()
return str(self) + " saved!"
return "Error adding class!"
@staticmethod
def delete_class(class_id):
delete_class_query = '''
DELETE FROM classes
WHERE class_id = {0}
'''.format(class_id)
if run_cursor.execute(delete_class_query):
connection.commit()
return str(class_id) + " deleted!"
return "Error deleting class ID: " + str(class_id)
@staticmethod
def get_all_classes():
get_all_classes_query = '''
SELECT * FROM classes
'''
if run_cursor.execute(get_all_classes_query):
classes = run_cursor.fetchall()
return classes
@staticmethod
def get_all_class_ids():
get_all_class_ids_query = '''
SELECT class_id FROM classes
'''
if run_cursor.execute(get_all_class_ids_query):
class_ids = [row[0] for row in run_cursor.fetchall()]
return class_ids
@staticmethod
def get_class_details(class_id):
get_class_details_query = '''
SELECT subject FROM classes WHERE class_id = {0}
'''.format(class_id)
if run_cursor.execute(get_class_details_query):
class_details = [row[0] for row in run_cursor.fetchall()]
return class_details
def __str__(self):
return "Class Subject: " + self.subject