cs project
cs project
CERTIFICATE EXAMINATION
2024-25
SUBMITTED BY
ANAND RODRIGUEZ MENON
XII-SCIENCE
1
CERTIFICATE
2
DECLARATION
XII-SCIENCE
3
Acknowledgment
4
profoundly grateful for their contributions, without which
this endeavor would not have been possible.
1 Introduction 6
2 Source Code 6
3 Screenshots 7
5 Conclusion 8
6 Bibliography 10
5
INTRODUCTION
6
SOURCE CODE
#Creating Database:
1. import mysql.connector as m
2. mydb = m.connect(
3. host='localhost',
4.
5. user='root',
6. password='ammaamrita'
7. )
8. mydb.autocommit=True
9.
10. c=mydb.cursor()
11.
12. # Create the database if it doesn't exist
13. c.execute('CREATE DATABASE IF NOT EXISTS school;')
14.
15. # Switch to the school database
16. c.execute('use school;')
17.
18. # Create tables if they don't exist
19. c.execute('''
20. CREATE TABLE IF NOT EXISTS login(
21. user_name VARCHAR(50),
22. password VARCHAR(50));''')
23.
24. c.execute('''
25. CREATE TABLE IF NOT EXISTS student(
26. Ad_no varchar(10) not null primary key,
27. name varchar(50),
28. class varchar(10),
29. gender CHAR(1) CHECK (gender IN ('M','F')),
30. mobile_no CHAR(10) CHECK (mobile_no REGEXP '^[0-9]{10}$'),
31. DOB date,
32. Father_name varchar(50),
33. Father_no char(10) CHECK (mobile_no REGEXP '^[0-9]{10}$'),
34. Mother_name varchar(100),
35. Mother_no char(10) CHECK (mobile_no REGEXP '^[0-9]{10}$'),
36. Address varchar(200));''')
37.
38. c.execute('''
39. CREATE TABLE IF NOT EXISTS staff(
40. ID CHAR(7) NOT NULL PRIMARY KEY,
41. password VARCHAR(20) NOT NULL,
42. Name VARCHAR(50),
43. Gender CHAR(1),
44. DOB date,
45. Mobile_no char(10) CHECK (mobile_no REGEXP '^[0-9]{10}$'),
46. Role varchar(20),
47. subject varchar(20),
48. Address varchar(200));''')
7
#Main code:
1. import mysql.connector as m
2. import keyring
3. import sys
4. import getpass
5. import msvcrt
6.
7. username=keyring.get_password('mysql','db_user')
8. password=keyring.get_password('mysql','db_password')
9.
10. try:
11. mydb = m.connect(
12. host='localhost',
13. user=username,
14. password=password,
15. database='school'
16. )
17. mydb.autocommit=True
18. except Exception as e:
19. print(f'Error: {e}')
20. exit()
21.
22. def masked_input(prompt='', mask='*'):
23. """Prompt for password input with masking."""
24. print(prompt, end='', flush=True)
25. password = ''
26.
27. while True:
28. char = msvcrt.getch() # Get a single character from input
29. if char in [b'\r', b'\n']: # Enter key pressed
30. break
31. elif char == b'\x08': # Backspace key pressed
32. if password:
33. password = password[:-1] # Remove last character
34. print('\b \b', end='', flush=True) # Move back, print space, move
back again
35. else:
36. password += char.decode('utf-8') # Add character to password
37. print(mask, end='', flush=True) # Print masking character
38.
39. print() # Move to next line
40. return password
41.
42. #Login page
43. def loginpage():
44. print('''
45. ----------
46. Login Page
47. ----------
48. ''')
49. print('1. Login\n2. Sign up')
50. ch=input('Enter choice: ').strip()
51. if ch=='1':
52. log_in()
53. elif ch=='2':
54. sign_up()
55. else:
56. print('Wrong command, Try again')
57. loginpage()
58.
59. #Function to sign up(add a new account)
8
60. def sign_up():
61. a_pword=masked_input('Enter Admin password: ').strip()
62. with mydb.cursor() as c:
63. c.execute('select password from login where user_name="admin";')
64. key=c.fetchone()[0]
65. if key == a_pword:
66. user = input('Enter new username: ').lower().strip()
67. passwd = masked_input('Enter password: ').strip()
68. passwd2 = masked_input('Enter password: ').strip()
69. if passwd2 == passwd:
70. try:
71. c.execute("INSERT INTO login (user_name, password) VALUES (%s,
%s)", (user,\
72.
passwd))
73. print('User added successfully')
74. homepage()
75. except Exception as e:
76. print(f'Error: {e}. Username might already exist.')
77. mydb.rollback()
78. else:
79. print('Passwords do not match. Please try again.')
80. loginpage()
81. else:
82. print('Invalid admin password. Access denied.')
83. loginpage()
84.
85. #Function to Login
86. def log_in():
87. with mydb.cursor() as c:
88. ruser=input('Enter ID: ').strip()
89. rpasswd=masked_input('Enter password: ').strip()
90. c.execute("SELECT * FROM login WHERE user_name = %s AND password = %s",
(ruser,\
91.
rpasswd))
92. data=c.fetchall()
93. if not data:
94. print('Invalid ID or password !!!')
95. loginpage()
96. else:
97. print('Login successful')
98. homepage()
99.
100. #Function for homepage
101. def homepage():
102. print('''
103. -----------------------------------
104. Welcome to School Management System
105. -----------------------------------''')
106. while True:
107. print('''
108. ********
109. Homepage
110. ********\n''')
111. print('1. Students\n2. Staff\n3. Logout\n')
112. ch=input('Enter your choice: ').strip()
113. if ch=='1':
114. mode='student'
115. student()
116. break
117. elif ch=='2':
118. staff()
119. break
9
120. elif ch=='3':
121. log=input('\nConfirm Logout(y/n): ').strip()
122. if log.lower()=='y':
123. print('Logged out successfully')
124. loginpage()
125. break
126. else:
127. pass
128. else:
129. print('Invalid choice')
130.
131. #Function for student page
132. def student():
133. mode='student'
134. tag='Ad_no'
135. print('''
136. --------
137. Students
138. --------''')
139. print('1. View\n2. Search\n3. Add\n4. Update\n5. Delete\n6. Back\n')
140. ch=input('Eneter your choice: ').strip()
141. if ch=='1':
142. view(mode,tag)
143. elif ch=='2':
144. search(mode,tag)
145. elif ch=='3':
146. add_s()
147. elif ch=='4':
148. update(mode,tag)
149. elif ch=='5':
150. delete(mode,tag)
151. elif ch=='6':
152. homepage()
153. else:
154. print('Invalid choice')
155. student()
156.
157. #Function for staff page
158. def staff():
159. mode='staff'
160. tag='ID'
161. print('''
162. -----
163. Staff
164. -----''')
165. print('1.View\n2. Search\n3. Add\n4. Update\n5. Delete\n6. Back\n')
166. ch=input('Eneter your choice: ').strip()
167. if ch=='1':
168. view(mode,tag)
169. elif ch=='2':
170. search(mode,tag)
171. elif ch=='3':
172. add_t()
173. elif ch=='4':
174. update(mode,tag)
175. elif ch=='5':
176. delete(mode,tag)
177. elif ch=='6':
178. homepage()
179. else:
180. print('Invalid choice')
181. staff()
182.
183. #Function to view records
10
184. def view(mode,tag):
185. with mydb.cursor() as c:
186. c.execute(f'select * from {mode} ')
187. r=c.fetchall()
188. for i in r:
189. print(i)
190. if mode=='student':
191. student()
192. elif mode=='staff':
193. staff()
194.
195. #Function to search records
196. def search(mode,tag):
197. search=input('Enter Name/"{}"to be searched: '.format(tag)).strip()
198.
199. with mydb.cursor() as c:
200. try:
201. c.execute(f'SELECT * FROM {mode} WHERE Name = %s;', (search,))
202. results = c.fetchall()
203.
204. if results:
205. for record in results:
206. print(record)
207. else:
208. if tag in ['Father_number', 'Mother_number']:
209. tag = f"`{tag}`"
210.
211. c.execute(f'SELECT * FROM {mode} WHERE {tag} = %s;', (search,))
212. results = c.fetchall()
213.
214. if results:
215. for record in results:
216. print(record)
217. else:
218. print('No records found for the given input.')
219.
220. except m.Error as err:
221. print(f"Error: {err}")
222.
223. if mode == 'student':
224. student()
225. elif mode == 'staff':
226. staff()
227.
228. #Function to update records
229. def update(mode,tag):
230. print('\nWhat do you want to update? ')
231. if mode=='student':
232. print('\n1. Name\n2. Class\n3. Gender\n4. DOB\n5. Father Name\n6. Father\
233. Number\n7. Mother Name\n8. Mother No\n9. Address\n10. Back\n')
234. elif mode=='staff':
235. print('\n1. Name\n2. Gender\n3. DOB\n4. Mobile Number\n5. Role\n6. Subject\
236. \n7.Address\n8.Back')
237. ch=input('Enter your choice: ')
238. opt=''
239. if ch=='1':
240. opt='Name'
241. elif ch=='2':
242. if mode=='student':opt='Class'
243. elif mode=='staff':opt='Gender'
244. elif ch=='3':
245. if mode=='student':opt='Gender'
246. elif mode=='staff':opt='DOB'
247. elif ch=='4':
11
248. if mode=='student':opt='DOB'
249. elif mode=='staff':opt='Mobile_no'
250. elif ch=='5':
251. if mode=='student':opt='Father_name'
252. elif mode=='staff':opt='Role'
253. elif ch=='6':
254. if mode=='student':opt='Father_no'
255. elif mode=='staff':opt='Subject'
256. elif ch=='7':
257. if mode=='student':opt='Mother_name'
258. elif mode=='staff':opt='Address'
259. elif ch=='8':
260. if mode=='student':opt='Mother_no'
261. elif mode=='staff':staff()
262. elif ch=='9':
263. if mode=='student':opt='Address'
264. elif mode=='staff':staff()
265. elif ch=='10':
266. if mode=='student':student()
267. elif mode=='staff':staff()
268. else:
269. print('Invalid choice')
270. update(mode,tag)
271. key = input(f'Enter {tag}: ').strip()
272. value=input('Enter the new value: ').strip()
273. with mydb.cursor() as c:
274. c.execute(f'UPDATE {mode} SET {opt} = %s WHERE {tag} = %s;', (value, key))
275. print('Record updated successfully')
276. if mode=='student':
277. student()
278. elif mode=='staff':
279. staff()
280.
281. #Function to delete record
282. def delete(mode,tag):
283. search = input(f'Enter Name/{tag} to be deleted: ').strip()
284. with mydb.cursor() as c:
285. c.execute(f'DELETE FROM {mode} WHERE Name = %s;', (search,))
286. r=c.fetchall()
287. if r!=[]:
288. print('Record deleted successfully')
289. else:
290. c.execute(f'DELETE FROM {mode} WHERE {tag} = %s;', (search,))
291. print('Records deleted successfully')
292. if mode=='student':
293. student()
294. elif mode=='staff':
295. staff()
296.
297. #Function to add new record for student
298. def add_s():
299. Ad_no=input('Enter Admission number of student: ').strip()
300. Name=input('Enter name of student: ').strip()
301. Class=input('Enter class of student: ').strip()
302. Gender=input('Enter gender of student(M/F): ').strip()
303. DOB=input('Enter date of birth of student(yyyy-mm-dd): ').strip()
304. F_name=input('Enter father\'s name: ').strip()
305. F_no=input('Enter father\'s number: ').strip()
306. M_name=input('Enter mother\'s name: ').strip()
307. M_no=input('Enter mother\'s number: ').strip()
308. Adrs=input('Enter student\'s Address: ').strip()
309. query = 'INSERT INTO student (Ad_no, Name, Class, Gender, DOB,\
310. Father_name, Father_no, Mother_name, Mother_no, Address) \
311. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s);'
12
312. with mydb.cursor() as c:
313. try:
314. c.execute(query, (Ad_no, Name, Class, Gender, DOB, F_name, F_no, M_name,
M_no, Adrs))
315. mydb.commit() # Commit the transaction
316. print('Record added successfully')
317. except m.Error as err:
318. print(f'Error: {err}') # Print specific database error
319. mydb.rollback() # Rollback in case of error
320. except Exception as e:
321. print(f'An unexpected error occurred: {e}')
322. c.close()
323. student()
324.
325. #Function to add record for staff
326. def add_t():
327. ID = input('Enter ID of staff: ')
328. Name = input('Enter name of staff: ')
329. Gender = input('Enter gender of staff (M/F): ')
330. DOB = input('Enter date of birth of staff (yyyy-mm-dd): ')
331. Mobile_no = input('Enter mobile number: ')
332. Role = input('Enter staff\'s role (teaching/non-teaching staff): ')
333. sub = input('Enter staff\'s subject/designation: ')
334. Adrs = input('Enter staff\'s Address: ')
335. Password = masked_input('Enter staff password: ').strip()
336. query = '''
337. INSERT INTO staff (ID, Name, Gender, DOB, Mobile_no, Role, subject, Address,
password)
338. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);
339. '''
340. with mydb.cursor() as c:
341. try:
342. c.execute(query, (ID, Name, Gender, DOB, Mobile_no, Role, sub, Adrs,
Password))
343. mydb.commit() # Commit the changes to the database
344. print('Staff record added successfully.')
345. except m.Error as err:
346. print(f"Error: {err}")
347. staff()
348. loginpage()
349.
13
SCREENSHOTS
Login page
14
Students page
15
Viewing a student record
16
Deleting a student record
17
Staff page
18
Viewing staff record
19
Deleting a staff record
20
Logging out
21
CONCLUSION
Looking ahead, the School Management System has the potential for
further enhancements, such as integrating attendance tracking, grade
management, and reporting functionalities. These improvements could
provide even greater value to educational institutions by equipping them
with comprehensive tools to meet their evolving needs. Ultimately, this
project serves as a solid foundation for continuous improvement in
educational administration, creating an environment where both educators
and students can flourish. By leveraging technology, the SMS contributes
to a more organized and effective educational experience, paving the way
for a brighter future in school management.
22
BIBLIOGRAPHY
https://stackoverflow.com/
Computer Science with Python (CBSE Class XII Textbook)
https://github.com/
23