I've got a CSV file and I wish to bulk-import this file into my sqlite3 database using Python. the command is ".import .....". however it appears it cannot work such as this. Can anybody produce a good example of how to get it done in sqlite3? I'm using home windows just just in case. Thanks
import csv, sqlite3 conn = sqlite3.connect(":memory:") curs = conn.cursor() curs.execute("CREATE TABLE t (col1 TEXT, col2 FLOAT);") with open('data.csv','rb') as infile: # csv.DictReader uses first line in file for column headings by default dr = csv.DictReader(infile, delimiter=',') to_db = [(i['col1'], i['col2']) for i in dr] curs.executemany("INSERT INTO t (col1, col2) VALUES (?, ?);", to_db)
.import command is really a feature from the sqlite3 command-line tool. To get it done in Python, you need to simply load the information using whatever facilities Python has, like the csv module, and placing the information according to usual.
By doing this, you might also need treatments for what types are placed, instead of depending on sqlite3's apparently undocumented behavior.