-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_python.py
More file actions
37 lines (33 loc) · 1.28 KB
/
db_python.py
File metadata and controls
37 lines (33 loc) · 1.28 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
import json
import sqlite3
from datetime import datetime
db=sqlite3.connect('example2.db')
with open('Mur3.json', encoding='utf-8-sig') as json_file:
json_data = json.loads(json_file.read())
#Aim of this block is to get the list of the columns in the JSON file.
columns = []
column = []
for data in json_data:
column = list(data.keys())
for col in column:
if col not in columns:
columns.append(col)
#Here we get values of the columns in the JSON file in the right order.
value = []
values = []
for data in json_data:
for i in columns:
value.append(str(dict(data).get(i)))
values.append(list(value))
value.clear()
#Time to generate the create and insert queries and apply it to the sqlite3 database
create_query = "create table if not exists myTable ({0})".format(" text,".join(columns))
insert_query = "insert into myTable ({0}), values (?{1})".format(",".join(columns), ",?" * (len(columns)-1))
print("insert has started at " + str(datetime.now()))
c = db.cursor()
c.execute(create_query)
c.executemany(insert_query, values)
values.clear()
db.commit()
c.close()
print("insert has completed at " + str(datetime.now()))