-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_to_database.py
44 lines (37 loc) · 1.14 KB
/
data_to_database.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
import json
import psycopg2
# Read the JSON file
with open('points.json') as f:
data = json.load(f)
# Connect to your PostgreSQL database
conn = psycopg2.connect(
dbname='test',
user='levi',
password='test',
host='localhost',
port='5433'
)
cur = conn.cursor()
# Insert data into the tables
for point in data['points']:
# Insert into points table
cur.execute(
"INSERT INTO soundmap_points (name, geom, color) VALUES (%s, POINT(%s, %s), %s) RETURNING id",
(point['name'], point['coordinates'][0], point['coordinates'][1], point['color'],)
)
point_id = cur.fetchone()[0]
# Insert into soundmap_image_paths table
for file_path in point['image_paths']:
cur.execute(
"INSERT INTO public.soundmap_image_paths (point_id, file_path) VALUES (%s, %s)",
(point_id, file_path)
)
# Insert into soundmap_audio_path table
cur.execute(
"INSERT INTO public.soundmap_audio_paths (id, file_path) VALUES (%s, %s)",
(point_id, point['audio_path'])
)
# Commit the changes and close the connection
conn.commit()
cur.close()
conn.close()