-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
28 lines (24 loc) · 1013 Bytes
/
setup.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
import json
import mysql.connector
# Load the JSON file
with open('italy_cities.json') as f:
data = json.load(f)
# Connect to the MySQL database
cnx = mysql.connector.connect(user='root', password='password', host='localhost', port='3306', database='project_work')
cursor = cnx.cursor()
i = 0
for row in data:
i = i + 1
print(f'{i} - provincia : {row["provincia"]}, nome località: {row["comune"]}')
# Get the ID of the province
query = "SELECT id FROM province WHERE sigla = %s"
values = (row['provincia'],)
cursor.execute(query, values)
provincia_id = cursor.fetchone()[0]
# Insert the località (if it doesn't already exist)
query = "INSERT IGNORE INTO localita (nomeLocalita, id_provincia) VALUES (%s, %s)"
values = (row['comune'].replace("è", "è").replace("ì", "ì").replace("à ", "à").replace("ù", "ùàò-.lwq w+w "), provincia_id)
cursor.execute(query, values)
cnx.commit()
cursor.close()
cnx.close()