-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
67 lines (61 loc) · 2.32 KB
/
db.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
import mysql.connector
from secrets import DB_SECRETS
from students import STUDENTS
class Database:
def __init__(self):
self.secrets = DB_SECRETS()
self.db = mysql.connector.connect(
host=self.secrets.host,
user=self.secrets.username,
password=self.secrets.password,
database=self.secrets.database
)
def fetch_all(self):
self. mycursor = self.db.cursor(dictionary=True)
self.mycursor.execute(
"SELECT * FROM {} ".format(self.secrets.table)
)
self.myresult = self.mycursor.fetchall()
self.fetched_student = []
for row in self.myresult:
self.fetched_student.append(
STUDENTS(
student_id=row['studentid'],
student_name=row['student_name'],
email=row['email'],
phone_number=row['phonenumber'],
student_address=row['student_address'],
entry_points=row['entrypoints']
).__dict__
)
return self.fetched_student
def fetch_records(self, admission_number):
self. mycursor = self.db.cursor(dictionary=True)
self.mycursor.execute(
"SELECT * FROM {} WHERE studentid={}".format(
self.secrets.table, admission_number)
)
self.myresult = self.mycursor.fetchall()
for row in self.myresult:
print(row)
self.fetched_student = STUDENTS(
student_id=row['studentid'],
student_name=row['student_name'],
email=row['email'],
phone_number=row['phonenumber'],
student_address=row['student_address'],
entry_points=row['entrypoints']
)
return self.fetched_student
def insert_records(self, student_name, email, phone_number, student_address, entry_points):
self.mycursor = self.db.cursor()
self.sql = "INSERT INTO {} (student_name,email,phonenumber,student_address,entrypoints) VALUES ({},{},{},{},{})".format(
self.secrets.table,
student_name,
email,
phone_number,
student_address,
entry_points
)
self.mycursor.execute(self.sql)
self.db.commit()