Import a CSV File into SQLite with .import
Load a CSV with sqlite3 .import --csv file table. If the table already exists the header row is inserted as data unless you add --skip 1 (3.32 or later).
You load a CSV file with a header line into an existing table, and the column names show up as the first record.
printf 'name,age\nann,30\nbob,25\n' > people.csv
sqlite3 app.db "create table people(name text, age int)"
sqlite3 app.db ".import --csv people.csv people" "select count(*) from people"
sqlite3 app.db "delete from people"
sqlite3 app.db ".import --csv --skip 1 people.csv people" "select count(*) from people"
sqlite3 app.db ".import --csv people.csv fresh" "select count(*) from fresh"3
2
2When the target table exists, .import treats every line of the file as data, so the header becomes a row and the count is three. With --skip 1 the first line is ignored and the count is two. When the table does not exist, .import creates it and uses the first line as column names, which is what the last command does. The behavior is described in the CSV import section of the shell documentation.
A created table has only text columns, so give the table a schema first if you need integers, and convert in a later step. --skip was added in version 3.32; on older shells, load into a fresh table and copy the rows. The importer needs the same number of fields on every line, or it reports the line number of the mismatch.