-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlCommand.py
More file actions
151 lines (114 loc) · 4.93 KB
/
Copy pathsqlCommand.py
File metadata and controls
151 lines (114 loc) · 4.93 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import json
import sys
from typing import List, Tuple
import mysql.connector
import pandas as pd
class MySQL:
def __init__(self, file="user.json"):
try:
self._connection = mysql.connector.connect(**self.get_account_info(file))
self.cursor = self._connection.cursor()
self.tableDict = self.get_table()
print("Connection success !")
except:
print("Connection Error: Check Internet Settings !")
def __del__(self):
self._connection.close()
def head(self, table: str):
self.cursor.execute(f"SELECT * FROM {table} LIMIT 5")
for i in self.cursor:
print(i)
def print_table(self, table: str):
self.cursor.execute(f"SELECT * FROM {table}")
for i in self.cursor:
print(i)
def insert_value(self, query: str, value: List[Tuple]):
self.cursor.executemany(query, value)
self._connection.commit()
print("Insert successed !")
def update_value(self, query: str):
self.cursor.execute(query)
self.cursor.commit()
print("Update successed !")
def create_table(self, tableName):
try:
self.cursor.execute(f"DROP TABLE IF EXISTS {tableName}")
self.cursor.execute(self.tableDict[tableName])
print(f"Table {tableName} created !")
except KeyError:
print("Failed to create table")
@staticmethod
def get_account_info(file: str):
with open(file, "r") as account:
user, password = json.load(account).values()
return {
"host": "db.cbgo4t4xbjoi.ap-southeast-1.rds.amazonaws.com",
"user": user,
"passwd": password,
"database": "db",
}
@staticmethod
def get_table():
with open("tableDict.json", "r") as table:
return json.load(table)
def read_csv(file: str) -> List[Tuple]:
df = pd.read_csv(file, encoding="ANSI")
return [tuple(i) for i in df.values.tolist()]
def init_child(file="csv/childCareSystem.csv"):
global db
db.create_table("childCareSystem")
data = read_csv(file)
SQL = "INSERT INTO `childCareSystem` (name, type, city, district, address, longitude, latitude,\
capacity, evaluate_year, evaluation_type, evaluation_result, \
rating_amount, rating_score, rating_average, google_rating)\
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
db.insert_value(SQL, data)
def init_child_demand(file="csv/predict_child.csv"):
global db
db.create_table("childSystemDemand")
data = read_csv(file)
SQL = """INSERT INTO `childSystemDemand` (town, town_id, year_99, year_100, year_101,
year_102, year_103, year_104, year_105, year_106, year_107, year_108, year_109,
year_110, year_111, year_112, year_113, year_114, year_115, year_116, year_117, year_118)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
db.insert_value(SQL, data)
def init_elderly(file="csv/elderlyCareSystem.csv"):
global db
db.create_table("elderlyCareSystem")
data = read_csv(file)
SQL = "INSERT INTO `elderlyCareSystem` (name, level, type, city, district, address,\
longitude, latitude, rating_amount, rating_score, rating_average, google_rating)\
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
db.insert_value(SQL, data)
def init_elderly_demand(file="csv/predict_old.csv"):
global db
db.create_table("elderlySystemDemand")
data = read_csv(file)
SQL = """INSERT INTO `childSystemDemand` (town, town_id, year_99, year_100, year_101,
year_102, year_103, year_104, year_105, year_106, year_107, year_108, year_109,
year_110, year_111, year_112, year_113, year_114, year_115, year_116, year_117, year_118)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"""
db.insert_value(SQL, data)
def init_powerbi(child="csv/powerbi_child.csv", elderly="csv/powerbi_elderly.csv"):
global db
db.create_table("powerbi_child")
db.create_table("powerbi_elderly")
childData = read_csv(child)
elderlyData = read_csv(elderly)
CHILDSQL = """INSERT INTO `powerbi_child` (county, city, district, town_id, longitude, latitude, year, predict)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s)"""
ELDERLYSQL = """INSERT INTO `powerbi_elderly` (county, city, district, town_id, longitude, latitude, year, predict)
VALUES(%s, %s, %s, %s, %s, %s, %s, %s)"""
db.insert_value(CHILDSQL, childData)
db.insert_value(ELDERLYSQL, elderlyData)
if __name__ == "__main__":
sys.stdout.reconfigure(encoding="utf-8")
db = MySQL()
db.create_table("childMessage")
db.create_table("elderlyMessage")
init_child()
init_elderly()
init_child_demand()
init_elderly_demand()
init_powerbi()
print("SQL Completed !")