If there are no headers in the .csv file, you do not want to use DictReader; DictReader assumes line 1 is a collection of headers and uses them as keys for each subsequent line. This is probably why you get KeyError s.
A modified version of the example from this link:
import csv, sqlite3 con = sqlite3.connect(":memory:") cur = con.cursor() cur.execute("CREATE TABLE t (col1, col2);") with open('data.csv','rb') as fin: dr = csv.reader(fin) dicts = ({'col1': line[0], 'col2': line[1]} for line in dr) to_db = ((i['col1'], i['col2']) for i in dicts) cur.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db) con.commit()
source share