/
load_csv.py
executable file
·47 lines (33 loc) · 1.13 KB
/
load_csv.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
#!/usr/bin/python
import csv
import sqlite3
import sys
filename = None
if len(sys.argv)==2:
filename = sys.argv[1]
print('File name to import :: {}'.format(filename))
else:
print('No file name provided or invalid input. Please run "./load_csv.py <csv-filename>"')
sys.exit()
def read_csv(filename):
try:
with open(filename) as csv_file:
csv_reader = csv.DictReader(csv_file)
for line in csv_reader:
yield line['name'], line['age']
except IOError:
raise IOError('{} filename not found!!'.format(filename))
conn = sqlite3.connect('noddus.db')
print("Opened database successfully")
#Not using string formatting here to avoid https://xkcd.com/327/ :)
query = "INSERT INTO PERSON(NAME, AGE) VALUES(?, ?)"
lines_inserted = 0
for data in read_csv(filename):
cur = conn.execute(query, (data))
lines_inserted += 1
conn.commit()
cursor = conn.execute('SELECT count(*) from PERSON')
total_records = cursor.fetchone()[0]
conn.close()
print("Closed database successfully")
print("{} records inserted, total records are {}.".format(lines_inserted, total_records))