-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_access.py
169 lines (142 loc) · 6.18 KB
/
database_access.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import mysql.connector
import decimal
db = mysql.connector.connect(
host = 'localhost',
user = 'root',
password = '',
database = 'attendance'
)
def process(res):
res = list(map(lambda x: list(x), res))
for i in range(len(res)):
for j in range(len(res[i])):
if type(res[i][j]) == decimal.Decimal:
res[i][j] = float(res[i][j])
return res
def get_total_students():
cursor.execute('select count(*) from student')
return cursor.fetchall()[0][0]
def get_total_teachers():
cursor.execute('select count(*) from teacher')
return cursor.fetchall()[0][0]
def get_all_class():
cursor.execute(f'select Class from student group by Class')
return cursor.fetchall()
def get_number_of_students_in_class(Class):
cursor.execute(f'select TotalStudentsInClass("{Class}")')
return cursor.fetchall()[0][0]
cursor = db.cursor()
def get_teacher_details(username):
cursor.execute(f'select Name, TRN from teacher where TRN = "{username}"')
return cursor.fetchall()
def get_student_details(username: str):
cursor.execute(f'select * from student where SRN = "{username}"')
return cursor.fetchall()
def get_avg_students():
cursor.execute('select avg(Age) from student')
return process(cursor.fetchall())[0][0]
def get_avg_teachers():
cursor.execute('select avg(Age) from teacher')
return process(cursor.fetchall())[0][0]
def authenticate_login(username: str, password: str, state: str):
match state:
case "Student":
cursor.execute(f'select * from login_student where User_name = "{username}" and passwd = "{password}"')
case "Teacher":
cursor.execute(f'select * from login_teacher where User_name = "{username}" and passwd = "{password}"')
case "Admin":
cursor.execute(f'select * from admin where User_name = "{username}" and passwd = "{password}"')
res = cursor.fetchall()
if len(res) != 0:
return True
return False
def course_of_student(username, doa):
cursor.execute(f'select class from student where SRN = "{username}"')
class_ = cursor.fetchall()[0][0]
cursor.execute(f'select Period_one, Period_two, Period_three, Period_four, Period_five from timetable where dow = DAYNAME("{doa}") and class = "{class_}"')
return cursor.fetchall()
def update_password(username, newPassword, state):
cursor.execute(f'CALL UpdatePassword("{username}", "{newPassword}", "{state}")')
def get_teacher_classes(username: str, course: str):
cursor.execute(f'select class from teaches where TRN = "{username}" and course_code = "{course}"')
return cursor.fetchall()
def get_students_of_class(c):
cursor.execute(f'select SRN, Name from student where Class = "{c}"')
return cursor.fetchall()
def get_teacher_courses(username):
cursor.execute(f'select course_code from teaches where TRN = "{username}"')
return cursor.fetchall()
def get_class_on_day(username, date, course):
cursor.execute(f"""select class from timetable t where dow = DAYNAME("{date}") and (Period_one = "{course}" or Period_two = "{course}" or Period_three = "{course}" or Period_four = "{course}" or Period_five = "{course}") and exists (
select * from teaches e where course_code = "{course}" and TRN = "{username}" and t.class = e.class
)""")
return cursor.fetchall()
def update_attendance(rows, course, doa):
for students in rows:
query = f'insert into attended values ("{students["SRN"]}", "{doa}", "{course}", "P");'
cursor.execute(query)
db.commit()
def get_attendance_details(username, doa):
cursor.execute(f'select course_code, status from attended where doa = "{doa}" and SRN = "{username}"')
return cursor.fetchall()
def get_attendance_status(username, course, doa):
cursor.execute(f'select status from attended where SRN = "{username}" and course_code = "{course}" and doa = "{doa}"')
return cursor.fetchall()
def get_tables():
cursor.execute('show tables')
return cursor.fetchall()
def columns(table):
cursor.execute(f"""
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = '{table}'
ORDER BY ORDINAL_POSITION
""")
return cursor.fetchall()
def insert(table, vals):
query = f'insert into {table} values('
for i in range(len(vals) - 1):
query += f'"{vals[i]}", '
if len(vals):
query += f'"{vals[len(vals) - 1]}")'
cursor.execute(query)
db.commit()
def read(table):
cursor.execute(f'select * from {table}')
return cursor.fetchall()
def update(table, vals, selected_row):
cols = list(selected_row[0].keys())[1:]
query = f'update {table} SET '
for i in range(len(vals) - 1):
query += f'{vals[i][0]} = "{vals[i][1]}", '
if len(vals):
query += f'{vals[len(vals) - 1][0]} = "{vals[len(vals) - 1][1]}" '
query += 'where '
d = dict(vals)
for i in range(len(cols)):
if cols[i] not in d.keys():
query += f'{cols[i]} = "{selected_row[0][cols[i]]}" and '
if len(cols):
query += f'{cols[len(cols) - 1]} = "{selected_row[0][cols[-1]]}"'
cursor.execute(query)
db.commit()
def delete(table, selected_rows):
for rows in selected_rows:
query = f"delete from {table} where "
conditions = list(rows.keys())[1:]
for i in range(len(conditions) - 1):
if rows[conditions[i]] == None:
query += f'{conditions[i]} = NULL and '
else:
query += f'{conditions[i]} = "{rows[conditions[i]]}" and '
if len(conditions):
if rows[conditions[len(conditions) - 1]] == None:
query += f'{conditions[len(conditions) - 1]} = NULL'
else:
query += f'{conditions[len(conditions) - 1]} = "{rows[conditions[len(conditions) - 1]]}"'
print(query)
cursor.execute(query)
db.commit()
def get_teachers_of_students(Class):
cursor.execute(f'select Name, course_code from teacher join teaches where teacher.TRN = teaches.TRN and teaches.Class = "{Class}"') #Join Query 1
return cursor.fetchall()