brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: .once sends the next SQLite query result to a file and then stops

Sqlite
~/tips/tip-sqlite-once-output-file

sqlite> .once users.csv

Writes one query result to a file from inside the shell, then output goes back to the screen.

Inside an interactive sqlite3 session, you sometimes want one result in a file without leaving the shell. .once redirects exactly one query.

sqlite> .mode csv
sqlite> .once users.csv
sqlite> SELECT id, email FROM users;
sqlite> SELECT 'back on screen';
back on screen

The result of the first query after .once goes to users.csv, and the next one shows on screen again. It uses whatever output mode is active, so set .mode first.

Two variations are fun. .once -x writes the next result as CSV to a temporary file and opens it in your spreadsheet app. .once -e opens it in a text editor instead. Both depend on what your system has set up to open files, so they’re more of a desktop trick than something for a server.

.output file.txt is the sibling that keeps sending everything to the file until you run .output with no name. I think that is easy to forget, and then your queries seem to print nothing. With .once that can’t happen.

The file is overwritten without a question, and the path is relative to the directory you started sqlite3 in, not the directory of the database. Use a full path when in doubt.

Further reading: Command Line Shell For SQLite

More Sqlite tips SELECT unixepoch('2026-01-01 00:00:00'); SELECT name, iif(qty > 0, 'in stock', 'sold out') AS state sqlite3 -bail app.db < migrate.sql CREATE INDEX 'orders_user_id' ON 'orders'('user_id'); –> users(id) CREATE INDEX users_active_email ON users(email) WHERE active = 1; All Sqlite tips (27) All topics

Link