My sqlite3 database contains a \"collate\" column-constraint. I\'ve placed it in the schema for the table, to prevent accidentally neglecting to use the necessary collation. How
The .import
command in the sqlite shell is a builtin command. It's processed by the shell program, not the SQL engine, so you can't execute it like an SQL statement.
Reading code for SQLite's shell.c, it seems that .import
is simply a loop, reading lines from the data file, splitting on the separator, and passing the fields as parameter values to a prepared INSERT
statement. So you should be able to mimic the behavior of .import
with Python code easily.
I tested the following with Python 2.6:
import sqlite3
import csv
conn = sqlite3.connect(':memory:')
conn.execute('create table mytable (col1 text, col2 text, col3 text)')
csvReader = csv.reader(open('mydata.csv'), delimiter=',', quotechar='"')
for row in csvReader:
conn.execute('insert into mytable (col1, col2, col3) values (?, ?, ?)', row)
cur = conn.cursor()
cur.execute('select * from mytable')
print cur.fetchall()