Export from sqlite to csv using shell script

I recently created a shell script that will be able to take the tables from a db file and convert them into csv files.

https://github.com/darrentu/convert-db-to-csv

Feel free to ask me any questions on my script :)


Instead of the dot commands, you could use sqlite3 command options:

sqlite3 -header -csv my_db.db "select * from my_table;" > out.csv

This makes it a one-liner.

Also, you can run a sql script file:

sqlite3 -header -csv my_db.db < my_script.sql > out.csv

Use sqlite3 -help to see the list of available options.


sqlite3

You have a separate call to sqlite3 for each line; by the time your select runs, your .out out.csv has been forgotten.

Try:

#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db <<!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!

instead.

sh/bash methods

You can either call your script with a redirection:

$ your_script >out.csv

or you can insert the following as a first line in your script:

exec >out.csv

The former method allows you to specify different filenames, while the latter outputs to a specific filename. In both cases the line .output out.csv can be ignored.

Tags:

Csv

Sqlite

Shell