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: sqlite3 -header -csv exports a query result from a database as CSV

Database
~/tips/tip-database-sqlite3-header-csv-export

sqlite3 -header -csv app.db "SELECT id, email FROM users" > users.csv

Exports SQLite query results to a CSV file and imports a CSV back with the shell.

Getting a table into a spreadsheet should not need a script. The sqlite3 shell can write CSV by itself.

sqlite3 -header -csv app.db "SELECT id, email FROM users" > users.csv
sqlite3 app.db ".import --csv --skip 1 users.csv users_copy"

-header adds the column names as the first line and -csv switches the output mode, so redirecting to a file gives a normal CSV. The second line goes the other way and loads a CSV into an existing table.

Without --skip 1 the header row would be imported as data when the table already exists. --skip needs a fairly recent SQLite (3.32 or newer), so check sqlite3 --version on older distributions.

Further reading: SQLite: Command Line Shell

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link