-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbf.py
More file actions
64 lines (54 loc) · 2.25 KB
/
dbf.py
File metadata and controls
64 lines (54 loc) · 2.25 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
"""
D ata
B ase
F unctions
Functions created for interacting with the database programmatically.
Works pretty nicely I think?
"""
import config
import mysql.connector
from mysql.connector import errorcode
class DatabaseFunctions:
# init a connection pool as to not completely flood the server with connectors lmao
def __init__(self):
try:
conf = config.Config()
self.cnx = mysql.connector.pooling.MySQLConnectionPool(pool_size=10,
pool_name="we_have_one_brain_cell",
host=conf.DB_HOST,
user=conf.DB_USER,
password=conf.DB_PASSWORD,
database=conf.DB_DATABASE
)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Access is denied. Check username and/or password.")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Bad database. Make sure the database exists.")
else:
print(err)
def get_school_ratings(self, school_id:int):
cnx = self.cnx.get_connection()
cursor = cnx.cursor()
# there's probably not 50,000 schools on rmp
cursor.execute("SELECT * from SCHOOL_INDIVIDUAL_RATING WHERE s_id = %s", (school_id,))
result = cursor.fetchall()
cursor.close()
cnx.close()
return result
def get_school_info(self, school_id:int):
cnx = self.cnx.get_connection()
cursor = cnx.cursor()
cursor.execute("SELECT * FROM school WHERE s_id = %s", (school_id,))
result = cursor.fetchall()
cursor.close()
cnx.close()
return result
def get_all_school_info(self):
cnx = self.cnx.get_connection()
cursor = cnx.cursor()
cursor.execute("SELECT * FROM school LIMIT 100000")
result = cursor.fetchall()
cursor.close()
cnx.close()
return result