-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
332 lines (282 loc) · 10.3 KB
/
Copy pathdb.py
File metadata and controls
332 lines (282 loc) · 10.3 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
""" db.py: This is where psycopg2 functions are defined. Most functions are
used to create the tables that make up the database and insert data.
"""
import psycopg2
import os
from helpers import *
def connect():
""" Connects to the server using psycopg2
and returns a connection object when called.
"""
try:
connection = psycopg2.connect(
host=os.environ['CLASS_DB_HOST'],
database=os.environ['CLASS_DB_USERNAME'],
user=os.environ['CLASS_DB_USERNAME'],
password=os.environ['CLASS_DB_PASSWORD']
)
return connection
except:
print ("Failed to connect to the database.")
def create_role_type():
""" Creates the person_role type. People can be writers, directors, or stars.
"""
conn = connect()
cur = conn.cursor()
create_stmt = "DROP TYPE IF EXISTS person_role; CREATE TYPE person_role AS ENUM ('writer', 'director', 'star');"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_persons_table():
""" Creates a table for persons in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE persons(" \
"id SERIAL PRIMARY KEY," \
"first_name varchar(32)," \
"last_name varchar(32));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_film_persons_table():
""" Creates a table for the ways people are associcated with films
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE film_persons(" \
"person_id INTEGER," \
"film_id INTEGER," \
"role_status person_role," \
"FOREIGN KEY (film_id) REFERENCES film (id)," \
"FOREIGN KEY (person_id) REFERENCES persons (id)," \
"PRIMARY KEY (person_id, film_id, role_status));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_company_table():
""" Creates a table for company in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE company(" \
"id SERIAL PRIMARY KEY," \
"company varchar(64));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_film_company_table():
""" Creates a table for film company relation in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE film_company(" \
"film_id INTEGER NOT NULL," \
"company_id INTEGER NOT NULL," \
"PRIMARY KEY (film_id)," \
"FOREIGN KEY (film_id) REFERENCES film (id)," \
"FOREIGN KEY (company_id) REFERENCES company (id));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_ratings_table():
""" Creates a table for ratings in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE rating(" \
"id SERIAL PRIMARY KEY," \
"rating_type varchar(10));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_film_table():
""" Creates a table for film in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE film(" \
"id SERIAL PRIMARY KEY," \
"title varchar(128)," \
"score DECIMAL(2, 1)," \
"release DATE," \
"budget float(1)," \
"gross float(1)," \
"votes INT," \
"rating INTEGER," \
"runtime INTEGER," \
"FOREIGN KEY (rating) REFERENCES rating (id));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_genre_table():
""" Creates a table for genre in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE genre(" \
"id SERIAL PRIMARY KEY," \
"genre_type varchar(32));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def create_film_genre_table():
""" Creates a table for film-genre relation in the database
"""
conn = connect()
cur = conn.cursor()
create_stmt = "CREATE TABLE film_genre(" \
"film_id INTEGER NOT NULL," \
"genre_id INTEGER NOT NULL," \
"PRIMARY KEY (film_id, genre_id)," \
"FOREIGN KEY (film_id) REFERENCES film (id)," \
"FOREIGN KEY (genre_id) REFERENCES genre (id));"
cur.execute(create_stmt)
conn.commit()
conn.close()
def insert_ratings(ratings_array):
conn = connect()
cur = conn.cursor()
for i in ratings_array:
cur.execute("INSERT INTO rating (rating_type) VALUES (%s)", (i,))
conn.commit()
conn.close()
def insert_film(name, score, date, budget, gross, votes, rating, runtime, cur):
if (rating != None):
cur.execute("SELECT id FROM rating WHERE rating_type = %s", (rating,))
rating_id = cur.fetchone()[0]
cur.execute("INSERT INTO film (title, score, release, budget, gross, votes, rating, runtime) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(name, score, date, budget, gross, votes, rating_id, runtime))
else:
cur.execute("INSERT INTO film (title, score, release, budget, gross, votes, rating, runtime) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(name, score, date, budget, gross, votes, None, runtime))
def insert_persons(persons):
""" Takes a Python list of stars and adds them to the database. Checks if
name is singular or has both a first and last name.
"""
conn = connect()
cur = conn.cursor()
for i in persons:
if type(i) == str and " " in i:
first_name = i.split()[0]
last_name = i.split()[1]
cur.execute("INSERT INTO persons (first_name, last_name) VALUES (%s, %s)", (first_name,last_name))
elif (type(i) == str and " " not in i):
name = i
cur.execute("INSERT INTO persons (first_name) VALUES (%s)", (name,))
else:
pass
conn.commit()
conn.close()
def insert_film_persons(a_list, role):
""" Takes a list of lists in the form [film_title, film_year, person_name] and inserts a
[film_id, person_id, role] record into the database.
"""
conn = connect()
cur = conn.cursor()
for row in a_list:
# Get the year in the row
date = row[1]
if type(row[1]) != float:
date = convert_date_to_postgres(date)
if (date is not None):
date = date.split("-")
year = date[0]
# Get the person_id using the person name
if type(row[2]) == str and " " in row[2]:
first_name = row[2].split()[0]
last_name = row[2].split()[1]
cur.execute("SELECT id FROM persons WHERE first_name = %s AND last_name = %s", (first_name, last_name))
person_id = cur.fetchone()
elif (type(row[2]) == str and " " not in row[2]):
first_name = row[2]
cur.execute("SELECT id FROM persons WHERE first_name = %s AND last_name IS NULL", (first_name,))
person_id = cur.fetchone()
# Get the film_id from the database using film_title and film_year
cur.execute("SELECT id FROM film WHERE title = %s AND EXTRACT(YEAR FROM release) = %s", (row[0], year))
film_id = cur.fetchone()
if (film_id is not None):
pass
# Insert the film_id, person_id and role into the many-to-many table
if (film_id is not None and person_id is not None):
cur.execute("INSERT INTO film_persons (person_id, film_id, role_status) VALUES (%s, %s, %s)", (person_id[0], film_id[0], role))
conn.commit()
conn.close()
def insert_companies(company):
""" Takes a Python list of companies and adds them to the database.
"""
conn = connect()
cur = conn.cursor()
for i in company:
if(i != ""):
cur.execute("INSERT INTO company (company) VALUES (%s)", i)
conn.commit()
conn.close()
def insert_film_company(company):
""" Takes a Python list of companies and searches for their ids
and adds them to the film ids to create an insert.
"""
conn = connect()
cur = conn.cursor()
cur.execute("SELECT id FROM film")
film_id = cur.fetchall()
spot = 0
for i in company:
if(i != i):
spot = spot + 1
else:
cur.execute("SELECT id FROM company WHERE company = %s", (i,))
company_id = cur.fetchone()[0]
cur.execute("INSERT INTO film_company (film_id, company_id) VALUES (%s, %s)", (film_id[spot], company_id,))
spot = spot + 1
conn.commit()
conn.close()
def insert_genres(genre):
""" Takes a Python list of genres and adds them to the genre databese
"""
conn = connect()
cur = conn.cursor()
for i in genre:
cur.execute("INSERT INTO genre (genre_type) VALUES (%s)", i)
conn.commit()
conn.close()
def insert_film_genre(genre):
""" Takes a Python list of genre and seaches for their ids
and inserts them into the file_genre table.
"""
conn = connect()
cur = conn.cursor()
cur.execute("SELECT id FROM film")
film_id = cur.fetchall()
spot = 0
for i in genre:
cur.execute("SELECT id FROM genre WHERE genre_type = %s", (i,))
genre_id = cur.fetchone()[0]
cur.execute("INSERT INTO film_genre (film_id, genre_id) VALUES (%s, %s)", (film_id[spot],genre_id,))
spot = spot + 1
conn.commit()
conn.close()
def do_query():
conn = connect()
cur = conn.cursor()
con_query = True
while con_query:
my_query = input("Enter your query: ")
try:
cur.execute(my_query)
#result = cur.fetchall()
result = cur.fetchmany(10)
for row in result:
print(row)
except (Exception, psycopg2.Error) as error:
print("Error fetching data from PostgreSQL table", error)
conn.commit()
conn.close()
conn = connect()
cur = conn.cursor()
another_query = input("\nWould you like to do another query (y/n): ")
if (another_query == "n"):
con_query = False
print("\nGood Bye")
conn.commit()
conn.close()