I installed sqlite3 and want to use it to recover information from stylish.sqlite which is located in my Firefox profile folder and is generated by the Stylish extension:

$ cd ~/.mozilla/firefox/w4wcp85s.default
$ sqlite3 stylish.sqlite
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
style_meta  styles
sqlite> SELECT * FROM styles;
6||||YouTube|/* AGENT_SHEET */ 
/* ▓▓ NIGHTSHIFT - eye care:                                 ▓▓
   ▓▓_http://userstyles.org/styles/18192/nightshift-eye-care_▓▓ */

@namespace url(http://www.w3.org/1999/xhtml);
@-moz-document regexp("https?://www.youtube.com/.*")  {
body,html {min-height: 100%!important; }
html, body{background-color:#111!important}

My question is this: how can I capture the output of sqlite> SELECT * FROM styles; to a file? (I know I can select the output in the terminal and copy it to a file.)


Run it all as a single command:

$ sqlite3 stylish.sqlite "SELECT * FROM styles;" > somefile.txt


$ sqlite3 addons.sqlite "select * from icon;" > somefile.txt

$ cat somefile.txt 

Using tee

If you want to see the output as it's being written to the file you can use tee instead.

$ sqlite3 addons.sqlite "select * from icon;" | tee somefile.txt

From inside the SQLite command line interface

If you are working inside the command line interface of SQLite, you cannot use redirections or pipes.
However, you can use the command .output (the command .mode could be useful too).

.output some_file
SELECT * FROM table; /* SQLite saves the output in 'some_file' */

SELECT * FROM table; /* SQLite presents the output again in the screen */

Two notes:

  • As soon you run .output some_file, the output file is opened in mode "w",
    thus be careful if the file already exists and contains some data.
  • To unset the redirection you must use the .output command without arguments.

An extra

You can produce a CSV file using the command .mode.

.mode csv
.output some_file
SELECT * FROM table;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy