-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_class.py
More file actions
202 lines (176 loc) · 7.5 KB
/
import_class.py
File metadata and controls
202 lines (176 loc) · 7.5 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
import sqlite3
import json
import csv
import xml
class import_class:
def import_file(self, filename):
split=filename.split(".")
if split[1]=="csv":
self.import_csv(self, filename)
elif split[1]=="json":
self.import_json(self, filename)
elif split[1]=="xml":
self.import_xml(self, filename)
def import_csv(self, filename):
count=0
conn = sqlite3.connect('data.sqlite')
catagories = []
csv_file = csv.reader(open(filename))
csv_dict_file = csv.DictReader(open(filename))
for row in csv_file:
count += 1
if count>2:
break
if count == 1:
catagories = row
elif count == 2:
tabExists = conn.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + filename[0:filename.index(".")]+"';")
result = tabExists.fetchone()[0]
if(result==0):
s = "CREATE TABLE "+filename[0:filename.index(".")] + \
" (id INTEGER NOT NULL, "
for index in range(0, len(catagories)):
i = catagories[index]
if row[index].isnumeric() and len(row[index]) <= 6:
s += i+" INTEGER "
elif "." in row[index]:
s += i+" REAL"
else:
s += i+" TEXT"
if index != len(catagories)-1:
s += ", "
else:
s += ', PRIMARY KEY("id" AUTOINCREMENT))'
#print(s)
conn.execute(s)
else:
conn.execute("DELETE FROM " + filename[0:filename.index(".")])
for row in csv_dict_file:
t = "INSERT INTO "+filename[0:filename.index(".")]+" ("
for index in range(0, len(catagories)):
i = catagories[index]
t += i
if index != len(catagories)-1:
t += ", "
else:
t += ")"
t += " VALUES ("
for index in range(0, len(catagories)):
i = catagories[index]
if(isinstance(row[i], str)):
t += '"' + row[i] + '"'
else:
t+= str(row[i])
if index != len(catagories)-1:
t += ", "
else:
t += ");"
#print(catagories)
#print(t)
conn.execute(t)
def import_json(self, filename):
_conn = sqlite3.connect('data.sqlite')
#START CODE TO CHECK IF TABLE EXISTS OR NOT, AND IF NOT THEN DELETE
tabExists = _conn.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + filename[0:filename.index(".")]+"';")
result = tabExists.fetchone()[0]
#print(result)
with open(filename,encoding='UTF-8') as json_file:
data = json.load(json_file)
#print(data)
if(result==0):
keys = data[0].keys()
s = "CREATE TABLE "+filename[0:filename.index(".")]+" (id INTEGER NOT NULL, "
for key in keys:
if key.isnumeric() and len(key)<=6:
s+= key+ " INTEGER,"
elif "." in key:
s+=key+" REAL,"
else:
s+=key+" TEXT,"
s+= 'PRIMARY KEY("id" AUTOINCREMENT))'
#print(s)
_conn.execute(s)
else:
_conn.execute("DELETE FROM " + filename[0:filename.index(".")])
#END CODE TO CHECK IF TABLE EXISTS OR NOT, AND IF NOT THEN DELETE
#FIRST PART OF INSERTING STATEMENT COMMAND
keys = data[0].keys()
initText = "INSERT INTO " + filename[0:filename.index(".")] +"("
for key in keys:
initText+=key+","
initText = initText[0:len(initText)-1]
initText+=') '
#INSERTING ALL VALUES
toExecute = ''
count = 0
for row in data:
toExecute = ''
count+=1
toExecute+=initText + " VALUES( "
for key in keys:
if(isinstance(row[key],str)):
toExecute+='"' + str(row[key]) + '",'
else:
toExecute+=str(row[key])+","
toExecute = toExecute[0:len(toExecute)-1]
toExecute+=");"
#print(toExecute + " -- " + str(count))
_conn.execute(toExecute)
#python runner.py cars.json
def import_xml(self, filename):
import xml.etree.ElementTree as ET
tree = ET.parse(filename)
root = tree.getroot()
catagories=[]
conn = sqlite3.connect('data.sqlite')
tabExists = conn.execute("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='" + filename[0:filename.index(".")]+"';")
result = tabExists.fetchone()[0]
if(result==0):
s = "CREATE TABLE "+filename[0:filename.index(".")] + \
" (id INT NOT NULL"
for elem in root.iter():
cata = str(elem)[str(elem).index("'")+1:str(elem).rindex("'")]
if not cata in catagories:
catagories.append(cata)
#print(catagories)
count=0
for st in root.findall('./record'):
count+=1
if count==1:
for index in range(0, len(catagories)):
i = catagories[index]
if st.find(i) is not None and st.find(i).text.isnumeric() and len(st.find(i).text) <= 6:
s += i+" INTEGER "
if st.find(i) is not None and "." in st.find(i).text:
s += i+" REAL"
else:
s += i+" TEXT"
if index != len(catagories)-1:
s += ", "
else:
s += 'PRIMARY KEY("id" AUTOINCREMENT))'
print(s)
conn.execute(s)
for cta in catagories:
t = "INSERT INTO "+filename[0:filename.index(".")]+" ("
for index in range(0, len(catagories)):
i = catagories[index]
t+=i
if index != len(catagories)-1:
t += ", "
else:
t += ")"
t += " VALUES ("
for index in range(0, len(catagories)):
i = catagories[index]
if st.find(i) is not None:
if(isinstance(t,str)):
t += '"' + st.find(i).text + '"'
else:
t+=st.find(i).text
if index != len(catagories)-1:
t += ", "
else:
t += ");"
#print(t)
conn.execute(t)