by yung_steezy · 5y ago · 0 comments
$ sqlite3
$ .mode csv
$ .import my.csv foo
$ SELECT * FROM foo WHERE name = 'bar';
It reads the header in automatically for the field names and then stores all the values as strings. PS> gsudo choco install sqlite3
PS > sqlite3
sqlite> .mode csv
sqlite> .separator ; \n
sqlite> .import my.csv foo
sqlite> SELECT * FROM foo WHERE name = 'bar';
For CSV that contain up to several 100k records native PowerShell solution is way faster and more practical: Get-Content my.csv | ConvertFrom-Csv | ? name -eq bar
FYI, I maintain miller choco package: https://community.chocolatey.org/packages/millerAnother advantage is that this can be executed to import as part of initialization and get to sqlite prompt with the table ready for sql execution:
$ csv_to_table.sh sample-test.csv
Or it can be executed using heredoc for non-interactive execution as well: $ csv_to_table.sh sample-test.csv <<-CMD
select * from sample_test limit 10;
CMD
... output ...