-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB_setup.py
72 lines (63 loc) · 2.28 KB
/
DB_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
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
68
69
70
71
72
import json
import mysql.connector.pooling
import re
# con = {
# "user": "debian-sys-maint",
# "password": "YNGJmkTnnhw4dDT2",
# "host": "localhost",
# "database": "taipei_day_trip"
# }
con = {
"user": "root",
"password": "root",
"host": "localhost",
"database": "taipei_day_trip"
}
connection_pool = mysql.connector.pooling.MySQLConnectionPool(
pool_name="my_pool",
pool_size=5,
**con
)
def execute_query(sql, values=None):
connection = None
try:
# Acquire a connection from the pool
connection = connection_pool.get_connection()
if connection.is_connected():
cursor = connection.cursor()
if values:
cursor.execute(sql, values)
connection.commit()
else:
cursor.execute(sql)
result = cursor.fetchall()
return result
except mysql.connector.Error as e:
print(f"Error: {e.msg}")
finally:
if connection:
# Close cursor and connection to release resources back to the pool
cursor.close()
connection.close()
url = "data/taipei-attractions.json"
with open(url, 'r', encoding='utf-8') as file:
data = json.load(file)
for i in range(len(data['result']['results'])):
name=data['result']['results'][i]['name']
category=data['result']['results'][i]['CAT']
description=data['result']['results'][i]['description']
address=data['result']['results'][i]['address']
address=address.replace(" ","")
transport=data['result']['results'][i]['direction']
mrt=data['result']['results'][i]['MRT']
lat=float(data['result']['results'][i]['latitude'])
lng=float(data['result']['results'][i]['longitude'])
url_string=data['result']['results'][i]['file']
file = url_string.split("https://")
images = [] # Reset the images list for each iteration
for j in file:
if j.endswith(".jpg") or j.endswith(".JPG"):
j = "https://" + j
images.append(j)
images_json=json.dumps(images)
execute_query("INSERT INTO taipei_spots (name, category, description, address, transport, mrt, lat, lng, images) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)", (name, category, description, address, transport, mrt, lat, lng, images_json))