-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_db.py
49 lines (41 loc) · 1.73 KB
/
create_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
46
47
48
49
import os
import psycopg2
import urlparse
import csvkit
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
db = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
#create tables
cur = db.cursor()
#genres table
cur.execute("CREATE TABLE genres (id serial PRIMARY KEY, databaseID integer, genre varchar);")
with open('data/genre.csv','rb') as fin:
dr = csvkit.unicsv.UnicodeCSVDictReader(fin)
to_db = [(i['databaseID'], i['genreText']) for i in dr]
cur.executemany("INSERT INTO genres (databaseID, genre) VALUES(%s, %s);", to_db)
#topics table
cur.execute("CREATE TABLE topics (id serial PRIMARY KEY, databaseID integer, topic varchar);")
with open('data/topicalSubject.csv','rb') as fin:
dr = csvkit.unicsv.UnicodeCSVDictReader(fin)
to_db = [(i['databaseID'], i['topicalSubjectText']) for i in dr]
cur.executemany("INSERT INTO topics (databaseID, topic) VALUES (%s, %s);", to_db)
#places table
cur.execute("CREATE TABLE places (id serial PRIMARY KEY, databaseID integer, place varchar);")
with open('data/geographicSubject.csv','rb') as fin:
dr = csvkit.unicsv.UnicodeCSVDictReader(fin)
to_db = [(i['databaseID'], i['geographicSubjectText']) for i in dr]
cur.executemany("INSERT INTO places (databaseID, place) VALUES (%s, %s);", to_db)
#names table
cur.execute("CREATE TABLE names (id serial PRIMARY KEY, databaseID integer, name varchar);")
with open('data/nameSubject.csv','rb') as fin:
dr = csvkit.unicsv.UnicodeCSVDictReader(fin)
to_db = [(i['databaseID'], i['nameSubjectText']) for i in dr]
cur.executemany("INSERT INTO names (databaseID, name) VALUES (%s, %s);", to_db)
db.commit()
db.close()