Skip to content

Instantly share code, notes, and snippets.

@nticaric
Created June 5, 2018 12:16
Show Gist options
  • Save nticaric/163034020c73c893d9aee4cd95f62df5 to your computer and use it in GitHub Desktop.
Save nticaric/163034020c73c893d9aee4cd95f62df5 to your computer and use it in GitHub Desktop.
Converting CSV to sqlite
# This example requires CSVkit (https://github.com/wireservice/csvkit). A Python toolset with a lot of very cool CSV tools.
# IMPORTANT NOTE: make sure to use a proper csv file. I had a lot of trouble with a csv file created by a service with Dutch
# as locale. Changing it to US solved the problem. Some locales use comma's to seperate point numbers. A semicolon is then
# used.
# Create table example and Load file-a.csv into it
csvsql --db sqlite:///example.db --table example --insert file-a.csv
# Add an extra file to table example
csvsql --db sqlite:///example.db --table example --no-create --insert file-b.csv
# Query the database with direct CSV output.
sql2csv --db sqlite:///example.db --query 'select avg(age) from example'
# Connect directly to the database
sqlite3 example.db
$ .tables # show the available tables
$ .schema example # show the structure of table example
$ select column-b, count(*) as total from example group by column-b having count(*) > 2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment