-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson_to_mysql.py
More file actions
247 lines (192 loc) · 4.88 KB
/
json_to_mysql.py
File metadata and controls
247 lines (192 loc) · 4.88 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import get_data as gd
import create_mysql_tables as cmt
table1 = {}
column1 = ''
new_tables = {}
keys = ''
values = ''
new_table = {}
mcolumn = []
mvalues = []
def mdict(mydict, name, c = 0):
global table1
global column1
global new_tables
global keys
global values
global new_table
global mcolumn
global mvalues
for key, value in mydict.items():
if type(value) is str or type(value) is int or type(value) is bool or type(value) is float:
table1[name+'_'+str(key)] = value
temp = []
for ij in str(key):
if ij == '-':
temp.append('_')
else:
temp.append(ij)
key = ''.join(temp)
mcolumn.append(name + '_' + str(key))
mvalues.append(value)
column1 += name + '_' + str(key) + ' '
keys += name + '_' + str(key) + ', '
if type(value) is str:
values += '"' + str(value) + '", '
column1 += 'varchar(500), '
elif type(value) is int:
values += str(value) + ', '
column1 += 'int(18), '
elif type(value) is float:
values += str(value) + ', '
column1 += 'decimal(19, 6), '
else:
values += str(value) + ', '
column1 += 'boolean, '
elif type(value) is dict:
nname = name+'_'+str(key)
mdict(value, nname, c)
else:
if c == 0:
new_tables[key] = value
else:
new_table[key] = value
def create_table_query(database, data):
global table1
global column1
global new_tables
global keys
global values
global new_table
global mcolumn
global mvalues
table1 = {}
column1 = ''
new_tables = {}
keys = ''
values = ''
new_table = {}
mcolumn = []
mvalues = []
i = 0
file = open('text.sql', 'w')
for key,value in data.items():
if type(value) is str or type(value) is int or type(value) is bool or type(value) is float:
table1[key] = value
temp = []
for ij in str(key):
if ij == '-':
temp.append('_')
else:
temp.append(ij)
key = ''.join(temp)
mcolumn.append(str(key))
mvalues.append(value)
column1 += str(key) + ' '
keys += str(key) + ', '
if type(value) is str:
values += '"' + str(value) + '", '
column1 += 'varchar(500), '
elif type(value) is int:
values += str(value) + ', '
column1 += 'int(18), '
elif type(value) is float:
values += str(value) + ', '
column1 += 'decimal(19, 6), '
else:
values += str(value) + ', '
column1 += 'boolean, '
elif type(value) is dict:
name = str(key)
mdict(value, name)
else:
new_tables[key] = value
tablename = 'table{}'.format(i)
i += 1
if len(column1) > 2:
column1 = column1[:-2]
query = "create table {}({})".format(tablename, column1)
file.write(query)
file.write('\n\n\n')
cmt.execute_query(database, query)
if len(keys) > 1:
keys = keys[:-2]
if len(values) > 1:
values = values[:-2]
cmt.insert_remaining_columns(database,tablename, mcolumn, mvalues)
query = "insert into {} ({}) values ({})".format(tablename, keys, values)
file.write(query)
file.write('\n\n\n')
cmt.execute_query(database, query, tt=1)
while True:
for key, value in new_tables.items():
mmm = 0
for val in value:
table1 = {}
column1 = ''
keys = ''
values = ''
new_table = {}
mcolumn = []
mvalues = []
if type(val) is str or type(val) is int or type(val) is bool or type(val) is float:
table1[key] = val
temp = []
for ij in str(key):
if ij == '-':
temp.append('_')
else:
temp.append(ij)
key = ''.join(temp)
mcolumn.append(str(key))
mvalues.append(value)
column1 += str(key) + ' '
keys += str(key) + ', '
if type(val) is str:
values += '"' + str(val) + '", '
column1 += 'varchar(100), '
elif type(val) is int:
values += str(val) + ', '
column1 += 'int(18), '
elif type(val) is float:
values += str(val) + ', '
column1 += 'decimal(19, 6), '
else:
values += str(val) + ', '
column1 += 'boolean, '
elif type(val) is dict:
name = str(key)
mdict(val, name, c = 1)
else:
new_table[key] = val
if len(column1) > 2:
column1 = column1[:-2]
if mmm == 0:
# tablename = 'table{}_{}'.format(i, str(key))
# i += 1
tablename = '{}'.format(str(key))
query = "create table {}({})".format(tablename, column1)
file.write(query)
file.write('\n\n\n')
cmt.execute_query(database, query)
mmm = 1
cmt.insert_remaining_columns(database, tablename, mcolumn, mvalues)
if len(keys) > 1:
keys = keys[:-2]
if len(values) > 1:
values = values[:-2]
query = "insert into {} ({}) values ({})".format(tablename, keys, values)
file.write(query)
file.write('\n\n\n')
cmt.execute_query(database, query, tt=1)
if not new_table:
break
else:
new_tables = new_table
def proc():
sanctionId = #list santion_id
for Id in sanctionId:
data = gd.get_data(Id)
database = cmt.create_database()
create_table_query(database, data)
proc()