-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_manager.py
34 lines (30 loc) · 1.11 KB
/
database_manager.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
import psycopg2
from pprint import pprint
class DataBaseConnection:
def __init__(self):
try:
self.connection = psycopg2.connect(
"dbname=test user=joe")
self.connection.autocommit = True
self.cursor = self.connection.cursor()
except:
pprint("Failed to connect to database")
# Add the scraped data to the database
def insertDB(self, list):
"""Insert todays songs into database"""
# List to unpack
self.mySongs = list
# get cursor object from database constructor
cur = self.cursor
# iterate through list of song dictionaries
for song in self.mySongs:
day = song['Day']
time = song['Time']
song_title = song['Song']
artist = song['Artist']
album = song['Album']
# Write variables to a row in our database
SQL = "INSERT INTO thecurrent (day, hour, song, artist,\
album) VALUES (%s, %s, %s, %s, %s);"
values = (day, time, song_title, artist, album)
cur.execute(SQL, values)