-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
45 lines (37 loc) · 1.12 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
from config import PG_URL
import psycopg2
def get_db():
return psycopg2.connect(PG_URL)
def create_tables(conn):
cur = conn.cursor()
print('dropping user_favorites')
cur.execute('DROP TABLE IF EXISTS user_favorites')
print('dropping songs')
cur.execute('DROP TABLE IF EXISTS songs')
print('dropping music_vectors')
cur.execute('DROP TABLE IF EXISTS music_vectors')
print('creating songs')
cur.execute('''
CREATE TABLE IF NOT EXISTS songs (
song_id INT PRIMARY KEY,
song_title varchar(1000),
band_name varchar(1000)
)
''')
print('creating user_favorites')
cur.execute('''
CREATE TABLE IF NOT EXISTS user_favorites (
user_id INT,
song_id INT,
FOREIGN KEY (song_id) REFERENCES songs (song_id)
)
''')
print('creating music_vectors')
cur.execute('''
CREATE TABLE IF NOT EXISTS music_vectors (
band_name varchar(200) PRIMARY KEY,
vector VECTOR(100)
)
''')
conn.commit()
conn.close()