forked from nikolaimerritt/london-tube-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.py
67 lines (52 loc) · 1.3 KB
/
load.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import psycopg2
import json
conn = psycopg2.connect(
dbname="vaticle",
user = "vaticle",
host = "localhost",
password = "postgres"
)
cur = conn.cursor()
cur.execute("DROP TABLE stations CASCADE;")
cur.execute("""
CREATE TABLE stations (
stationId VARCHAR PRIMARY KEY,
name VARCHAR,
latitude FLOAT,
longitude FLOAT
);""")
cur.execute("DROP TABLE lines CASCADE;")
cur.execute("""
CREATE TABLE lines (
lineId INTEGER PRIMARY KEY,
name VARCHAR
);""")
cur.execute("DROP TABLE connections;")
cur.execute("""
CREATE TABLE connections (
lineId INTEGER REFERENCES lines(lineId),
stationId VARCHAR REFERENCES stations(stationId),
PRIMARY KEY(lineId, stationId)
);""")
f = open('train-network.json')
data = json.load(f)
for i in data['stations']:
cur.execute("""
INSERT INTO stations (stationId, name, latitude, longitude)
VALUES(%s, %s, %s, %s);
""", (i['id'], i['name'], i['latitude'], i['longitude']))
lineId = 0
for i in data['lines']:
cur.execute("""
INSERT INTO lines (lineId, name)
VALUES(%s, %s);
""", (lineId, i['name']))
for j in i['stations']:
cur.execute("""
INSERT INTO connections (lineId, stationId)
VALUES(%s, %s);
""", (lineId, j))
lineId += 1
f.close()
cur.close()
conn.commit()