-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseConnection.py
More file actions
213 lines (184 loc) · 7.6 KB
/
Copy pathDatabaseConnection.py
File metadata and controls
213 lines (184 loc) · 7.6 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import psycopg2
class DatabaseConnection:
def __init__(self, connect_args=None):
with open('password.txt') as f:
password = f.readline()
try:
if connect_args is None:
self.connection = psycopg2.connect('dbname=jeopardy user=luke password=' + password)
else:
self.connection = psycopg2.connect(connect_args)
self.connection.autocommit = True
self.cursor = self.connection.cursor()
except psycopg2.Error as e:
print("Error connecting to database" + '\n' + e.pgcode + '\n' + e.pgerror)
def close(self):
try:
self.cursor.close()
self.connection.close()
except psycopg2.Error as e:
print("Error closing the connection." + '\n' + e.pgcode + '\n' + e.pgerror)
def insert_parsed_game(self, episode_num, game_link):
print('Inserting game into parsed list')
self.cursor.execute('''
INSERT INTO parsed_games(episode_num, game_link)
VALUES (%s, %s);
''', (episode_num, game_link))
def insert_season(self, name, start_date, end_date, total_games):
print('Inserting season')
self.cursor.execute('''
INSERT INTO seasons (season_name, start_date, end_date, total_games)
VALUES (%s, %s, %s, %s);
''', (name, start_date, end_date, total_games))
def insert_contestant(self, name, notes, games_played, total_winnings):
print('Inserting contestant')
self.cursor.execute('''
INSERT INTO contestants (name, notes, games_played, total_winnings)
VALUES (%s, %s, %s, %s);
''', (name, notes, games_played, total_winnings))
def insert_game(self, episode_num, season_id, air_date, notes, contestant1, contestant2, contestant3,
winner, score1, score2, score3):
print(f'Inserting game {episode_num}')
self.cursor.execute('''
INSERT INTO
games (episode_num, season_id, air_date, notes, contestant1, contestant2, contestant3, winner, score1, score2, score3)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);
''', (episode_num, season_id, air_date, notes, contestant1, contestant2, contestant3, winner,
score1, score2, score3))
def insert_question(self, game_id, value, daily_double, round, category, clue, response):
# print('Inserting clue')
self.cursor.execute('''
INSERT INTO clues (game_id, value, daily_double, round, category, clue, response)
VALUES (%s, %s, %s, %s, %s, %s, %s);
''', (game_id, value, daily_double, round, category, clue, response))
def print_seasons(self):
self.cursor.execute('SELECT * FROM seasons ORDER BY id;')
seasons = self.cursor.fetchall()
for season in seasons:
print(season)
def print_contestants(self):
self.cursor.execute('SELECT * FROM contestants ORDER BY id;')
contestants = self.cursor.fetchall()
for contestant in contestants:
print(contestant)
def print_games(self):
self.cursor.execute('SELECT * FROM games ORDER BY id;')
games = self.cursor.fetchall()
for game in games:
print(game)
def print_questions(self):
self.cursor.execute('SELECT * FROM clues ORDER BY id;')
questions = self.cursor.fetchall()
for question in questions:
print(question)
def game_parsed(self, episode_num):
self.cursor.execute('SELECT * FROM parsed_games WHERE episode_num=(%s)', (episode_num, ))
episode_num_found = len(self.cursor.fetchall()) > 0
return episode_num_found
def update_contestant(self, name, notes=None, games_played=None, total_winnings=None):
if games_played is not None:
self.cursor.execute('''
UPDATE contestants
SET games_played = (%s)
WHERE name = (%s);''', (games_played, name))
if notes is not None:
self.cursor.execute('''
UPDATE contestants
SET notes = (%s)
WHERE name = (%s);''', (notes, name))
if total_winnings is not None:
self.cursor.execute('''
UPDATE contestants
SET total_winnings = (%s)
WHERE name = (%s);''', (total_winnings, name))
def contestant_exists(self, name):
self.cursor.execute('''SELECT * FROM contestants WHERE name = (%s);''', (name,))
contestant = self.cursor.fetchone()
return contestant is not None
def get_contestant_games_played(self, name):
self.cursor.execute('''SELECT games_played FROM contestants WHERE name = (%s);''', (name,))
total_winnings = self.cursor.fetchone()
return total_winnings[0]
def get_contestant_winnings(self, name):
self.cursor.execute('''SELECT total_winnings FROM contestants WHERE name = (%s);''', (name,))
total_winnings = self.cursor.fetchone()
return total_winnings[0]
def get_contestant_id_from_name(self, name):
self.cursor.execute('''SELECT id FROM contestants WHERE name = (%s);''', (name,))
total_winnings = self.cursor.fetchone()
return total_winnings[0]
def get_game_from_episode_number(self, episode_number):
self.cursor.execute('''SELECT id FROM games WHERE episode_num = (%s);''', (episode_number,))
game_id = self.cursor.fetchone()
return game_id[0]
def setup_database(self):
# Parsed Games Table
self.cursor.execute('''
CREATE TABLE parsed_games (
id serial PRIMARY KEY,
episode_num integer,
game_link VARCHAR
);
''')
# Seasons Table
self.cursor.execute('''
CREATE TABLE seasons (
id serial PRIMARY KEY,
season_name VARCHAR(16),
start_date DATE,
end_date DATE,
total_games integer
);
''')
# Contestants Table
self.cursor.execute('''
CREATE TABLE contestants (
id serial PRIMARY KEY,
name VARCHAR NOT NULL,
notes VARCHAR,
games_played integer NOT NULL,
total_winnings integer
);
''')
# Games Table
self.cursor.execute('''
CREATE TABLE games (
id serial PRIMARY KEY,
episode_num INT UNIQUE,
season_id INT,
air_date DATE NOT NULL,
notes VARCHAR,
contestant1 INT,
contestant2 INT,
contestant3 INT,
winner INT,
score1 INT,
score2 INT,
score3 INT,
FOREIGN KEY (season_id) REFERENCES seasons (id),
FOREIGN KEY (contestant1) REFERENCES contestants (id),
FOREIGN KEY (contestant2) REFERENCES contestants (id),
FOREIGN KEY (contestant3) REFERENCES contestants (id),
FOREIGN KEY (winner) REFERENCES contestants (id)
);
''')
# Questions Table
self.cursor.execute('''
CREATE TABLE clues (
id serial PRIMARY KEY,
game_id INT,
value INT NOT NULL,
daily_double BOOLEAN NOT NULL,
round VARCHAR NOT NULL,
category VARCHAR NOT NULL,
clue VARCHAR NOT NULL,
response VARCHAR NOT NULL,
FOREIGN KEY (game_id) REFERENCES games (id)
);
''')
def delete_database(self):
self.cursor.execute('DROP TABLE games CASCADE')
self.cursor.execute('DROP TABLE seasons CASCADE')
self.cursor.execute('DROP TABLE contestants CASCADE')
self.cursor.execute('DROP TABLE clues CASCADE')
self.cursor.execute('DROP TABLE parsed_games')