Code Self Study Forum

Query CSV files with SQL

Basically, just start up sqlite3 in a terminal and type these commands:

.mode csv
.import filename.csv table_name
.schema table_name
SELECT * FROM table_name LIMIT 10;

I’ve imported CSV files into Postgres and MariaDB/MySQL, but I don’t think they automatically create the tables like sqlite does.

Another way is with R:

sheet <- read.csv("filename.csv")
sqldf("SELECT * FROM sheet LIMIT 10")

I started poking around at it after reading the comments here. It’s worth scanning there for additional ideas. pandasql (Python), dplyr (R), and sqlitebrowser (GUI) are also mentioned.


1 Like