Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export from sqlite to csv using shell script

Tags:

shell

sqlite

csv

I'm making a shell script to export a sqlite query to a csv file, just like this:

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

When executing the script, the output apears on the screen, instead of being saved to "out.csv". It's working doing the same method with the command line, but I don't know why the shell script fails to export data to the file.

What am I doing wrong?

like image 512
Rorro Avatar asked Apr 25 '11 08:04

Rorro


People also ask

Which of the following command is used to export data to CSV in SQLite?

In SQLite, by using “. output” command we can export data from database tables to CSV or excel external files based on our requirement.

How do I convert SQLite to excel?

Conversion from SQLite to XLSXUpload your SQLite data (widely used in software like SQLite Database) and convert them by one click to XLSX format (widely used in software like MS Excel). Notice to XLSX format - In case your data are POINT type, then XY coordinates will be exported as well.


1 Answers

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.

like image 71
Hollister Avatar answered Sep 24 '22 04:09

Hollister