Sqlite3 and bash, database settings

My bash script automates data entry into sqlite database. The problem I'm running into is that every time I change the database settings, the changes are lost when I run the next command. An example will explain this.

sqlite3 Correlate.db ".mode csv"
sqlite3 Correlate.db ".output Correlated.csv"
sqlite3 Correlate.db "SELECT * FROM ALL_Data;
sqlite3 Correlate.db ".show"

      

The end result .show

looks like this:

     echo: off
  explain: off
  headers: off
     mode: list
nullvalue: ""
   output: stdout
separator: "|"
    stats: off
    width: 

      

It seems that my change to csv output mode was lost. Also my output file has no data, although stdout prints the data I want to see. Is this resetting the database settings every time I call it? I'm pretty confused!

+3


source to share


1 answer


I would try something like this to send multiple commands to a single instance sqlite3

:

sqlite3 Correlate.db <<EOF
.mode csv
.output Correlated.csv
SELECT * FROM ALL_Data;
.show
EOF

      



You can also put these commands in a file and redirect it to sqlite3

, for example:

sqlite3 Correlate.db < commandfile

      

+4


source







All Articles