-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbmanager.py
105 lines (85 loc) · 3.22 KB
/
dbmanager.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
import mysql.connector
from datetime import datetime
from connection import connection
from Student import Student
from Teacher import Teacher
from Class import Class
class Dbmanager:
def __init__(self):
self.connection = connection
self.cursor = self.connection.cursor()
def GetStudentById(self, id):
sql = 'Select * From student where id = %s'
value = (id,)
self.cursor.execute(sql, value)
try:
obj = self.cursor.fetchone()
# print(obj)
return Student(obj[0], obj[1], obj[2], obj[3], obj[4], obj[5], obj[6])
except mysql.connector.Error as err:
print('Error: ', err)
def GetStudentsByClassId(self, classid):
sql = 'Select * From student where classid = %s'
value = (classid,)
self.cursor.execute(sql, value)
liste = []
try:
obj = self.cursor.fetchall()
# print(obj)
for i in obj:
liste.append(Student(i[0], i[1], i[2], i[3], i[4], i[5], i[6]))
return liste
except mysql.connector.Error as err:
print('Error: ', err)
def AddStudent(self, student: Student):
sql = 'INSERT INTO Student(StudentNumber, Name, Surname, Birthdate, Gender, Classid) VALUES (%s, %s, %s, %s, %s, %s)'
value = (student.studentnumber, student.name, student.surname, student.birthdate, student.gender, student.classid)
self.cursor.execute(sql, value)
try:
self.connection.commit()
print(f'{self.cursor.rowcount} tane kayıt eklendi.')
except mysql.connector.Error as err:
print('Hata: ', err)
def EditStudent(self, student: Student):
sql = 'update student set studentnumber = %s, name = %s, surname = %s, birthdate = %s, gender = %s, classid = %s where id = %s'
value = (student.studentnumber, student.name, student.surname, student.birthdate, student.gender, student.classid, student.id)
self.cursor.execute(sql, value)
try:
self.connection.commit()
print(f'{self.cursor.rowcount} tane kayıt güncellendi.')
except mysql.connector.Error as err:
print('Hata: ', err)
def AddTeacher(self, teacher: Teacher):
pass
def EditTeacher(self, teacher: Teacher):
pass
def getClasses(self):
sql = 'select * from class'
self.cursor.execute(sql)
liste = []
try:
obj = self.cursor.fetchall()
# print(obj)
for i in obj:
liste.append(Class(i[0], i[1], i[2]))
return liste
except mysql.connector.Error as err:
print('Error: ', err)
def __delattr__(self):
self.connection.close()
print('Db kapandı.')
# db = Dbmanager()
# clas = db.getClasses()
# print(clas[0].name)
# Student = db.GetStudentById(2)
# Student.name = 'Muhammed'
# Student.surname = 'NAS'
# Student.studentnumber = '0229'
# db.AddStudent(Student)
# std = Student(None, 297, 'Abdi', 'NAS', None, 'E', 1 )
# db.AddStudent(std)
# Student = db.GetStudentById(2)
# Student.name = 'Mustafa'
# Student.surname = 'NAS'
# Student.studentnumber = '0197'
# db.EditStudent(Student)