How do I use the SQLite CLI's "--skip 1" option?
Asked Answered
H

1

8

I'm importing several CSV files into a single table. The documentation for CSV import says

when the table already exists, every row of the CSV file, including the first row, is assumed to be actual content. If the CSV file contains an initial row of column labels, you can cause the .import command to skip that initial row using the "--skip 1" option.

But I can't seem to figure out a valid way to pass that flag. I tried the following:

sqlite> .import foo.csv contributions --skip 1
Usage: .import FILE TABLE

sqlite> .import --skip 1 foo.csv contributions
Usage: .import FILE TABLE

sqlite> .import foo.csv --skip 1 contributions
Usage: .import FILE TABLE

I'm using version 3.30.1.

Huddersfield answered 24/5, 2020 at 4:19 Comment(1)
Thanks for asking, because I was about to throw my hands up and use tail -n+2 or sed. I feel like the documentation could do a better job of calling this out, what versions which features got added. Some of the systems I work with have quite old versions of SQLite, so it's a too-common occurrence to find out "oh, I guess I can't use that."Hangchow
S
9

I find that on 3.32.0, this will work to skip the first two rows, the header row and the first data row:

.import --csv --skip 2 file.csv tablename

The issue may be where you say:

I'm using 3.30.1

According to the sqlite release notes, the --skip option was implemented in 3.32.0:

SQLite Release 3.32.0 On 2020-05-29 ... 9. Enhancements to the CLI: (a) Add options to the .import command: --csv, --ascii, --skip

You can get the newest versions from the official download page.

Scraggly answered 24/5, 2020 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.