-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.py
More file actions
199 lines (172 loc) · 6.78 KB
/
practice.py
File metadata and controls
199 lines (172 loc) · 6.78 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"""
Student and Attendance Management Module.
Handles student registration, attendance marking, and database operations.
"""
import sqlite3
import datetime
import time
from utilities import get_staff, sanitize_table_name
class AttendanceManager:
"""Manages attendance records and student information."""
def __init__(self, db_path='Attendance.db'):
"""
Initialize the AttendanceManager.
Args:
db_path: Path to SQLite database file
"""
self.conn = sqlite3.connect(db_path)
self.cur = self.conn.cursor()
def add_student(self, roll_num, name, email, city, country, phone, dob):
"""
Register a new student in the system.
Adds student to main Students table and all subject attendance tables.
Args:
roll_num: Student's roll number
name: Student's full name
email: Student's email address
city: Student's city
country: Student's country
phone: Student's phone number
dob: Student's date of birth
Returns:
True if successful, False otherwise
"""
try:
# Create Students table
self.cur.execute("""
CREATE TABLE IF NOT EXISTS Students (
roll_no TEXT PRIMARY KEY,
name TEXT,
email TEXT,
city TEXT,
country TEXT,
phone TEXT,
dob TEXT
)
""")
# Insert student with parameterized query for safety
self.cur.execute(
"INSERT INTO Students VALUES (?, ?, ?, ?, ?, ?, ?)",
(roll_num, name, email, city, country, phone, dob)
)
# Create or get Attendance table
self.cur.execute("""
CREATE TABLE IF NOT EXISTS Attendance (
roll_no VARCHAR(30) PRIMARY KEY,
name TEXT
)
""")
self.cur.execute("INSERT INTO Attendance VALUES (?, ?)", (roll_num, name))
# Add student to all existing subject tables
try:
self.cur.execute("SELECT * FROM STAFF")
staff_list = self.cur.fetchall()
for staff in staff_list:
subject = staff[2]
subject_table = sanitize_table_name(subject)
self.cur.execute(
f"INSERT INTO {subject_table} VALUES (?, ?)",
(roll_num, name)
)
except sqlite3.Error:
# No existing staff/subjects yet
pass
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error adding student {roll_num}: {str(e)}")
return False
def add_staff(self, roll_num, name, subject):
"""
Register a new staff member (teacher) in the system.
Creates a subject-specific attendance table.
Args:
roll_num: Staff member's roll number
name: Staff member's full name
subject: Subject they teach
Returns:
True if successful, False otherwise
"""
try:
# Create STAFF table
self.cur.execute("""
CREATE TABLE IF NOT EXISTS STAFF (
Roll_Number VARCHAR(30) PRIMARY KEY,
Name CHAR(30) NOT NULL,
Subject CHAR(30) NOT NULL
)
""")
# Insert staff with parameterized query
self.cur.execute("INSERT INTO STAFF VALUES (?, ?, ?)", (roll_num, name, subject))
# Create subject-specific attendance table
subject_table = sanitize_table_name(subject)
try:
self.cur.execute(f"""
CREATE TABLE IF NOT EXISTS {subject_table} AS
SELECT roll_no, name FROM Students
""")
except sqlite3.OperationalError:
# Table might exist or students table might be empty
self.cur.execute(f"""
CREATE TABLE IF NOT EXISTS {subject_table} (
roll_no VARCHAR(30) PRIMARY KEY,
name CHAR(30) NOT NULL
)
""")
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error adding staff {roll_num}: {str(e)}")
return False
def reset_database(self):
"""
Delete all tables from the database.
WARNING: This operation cannot be undone!
Returns:
True if successful, False otherwise
"""
try:
# Drop Students table
try:
self.cur.execute("DROP TABLE Students")
except sqlite3.OperationalError:
pass
# Drop all subject tables
try:
self.cur.execute("SELECT * FROM STAFF")
staff_list = self.cur.fetchall()
for staff in staff_list:
subject = staff[2]
subject_table = sanitize_table_name(subject)
try:
self.cur.execute(f"DROP TABLE {subject_table}")
self.cur.execute(f"DROP TABLE {subject_table}_col")
except sqlite3.OperationalError:
pass
except sqlite3.OperationalError:
pass
# Drop STAFF and Attendance tables
for table in ['STAFF', 'Attendance']:
try:
self.cur.execute(f"DROP TABLE {table}")
except sqlite3.OperationalError:
pass
self.conn.commit()
return True
except sqlite3.Error as e:
print(f"Error resetting database: {str(e)}")
return False
def close(self):
"""Close database connection."""
self.conn.close()
# Keep the old function names for backward compatibility with main.py
def Student_Details(conn, cur, roll_num, name, email, city, country, phone, dob):
"""Legacy function wrapper for add_student."""
manager = AttendanceManager('Attendance.db')
manager.add_student(roll_num, name, email, city, country, phone, dob)
manager.close()
def Add_Staff(conn, cur, roll_num, name, subject):
"""Legacy function wrapper for add_staff."""
manager = AttendanceManager('Attendance.db')
manager.add_staff(roll_num, name, subject)
manager.close()