To save the result of a SQLite query as a CSV file, use the sqlite3 command-line shell. It runs SQL statements and formats returned rows as comma-separated text.
This article shows how to choose columns and send one query’s result to a file. It also explains how to keep output directed to a file for several queries.
TL;DR
Run a SELECT query in the sqlite3 shell with CSV mode enabled, then send that query’s output to a file with the one-query redirection command.
sqlite3 shop.db .headers on .mode csv .once customers.csv SELECT id, name, balance FROM customers; .quit
- Use the shell command .headers on to include column names.
- Use the shell command .mode csv to format query results as CSV.
- Use the one-query command for one query or the persistent output command for ongoing redirection.
What is SQLite CSV export?
SQLite CSV export means writing the rows returned by a SQL query into a comma-separated text file. The sqlite3 command-line shell is a program that sends SQL statements to SQLite and formats their results.
The shell’s .mode csv setting controls the output format. It does not export every table automatically. A SELECT statement determines which columns and rows go into the file.
The SQLite shell documentation says CSV mode follows RFC 4180 formatting. You can export a whole table with SELECT *, or name specific columns to keep the file focused. See the SQLite CLI export documentation.
How to export a SQLite query to CSV
Open the database with sqlite3, enable CSV output and headers, then direct the next query to the file you want to create. I used a small customers table to check the output, including a note with a comma and quotes.
Step 1: Open the database and select columns
Pass the database filename to the sqlite3 program. The shell opens that file. Then use a SELECT statement to choose the fields and rows you need.
sqlite3 shop.db
At the sqlite prompt, enter the remaining dot-commands and query. A dot-command controls the shell, while SELECT is SQL sent to the database.
Step 2: Turn on headers and CSV mode
Enable column labels with .headers on, then select CSV formatting with .mode csv. These are shell dot-commands, not SQL statements.
.headers on .mode csv
For column labels, the documented CSV mode supports a titles option. The simpler headers command also adds labels, as shown in the export example above.
Step 3: Write the query result to a file
Use one-query command customers.csv to send the next SQL statement’s output to that path. Then run the query and close the shell.
.once customers.csv SELECT id, name, balance FROM customers; .quit
Choose an absolute path if you need the file in a specific directory. The path is interpreted by the shell process, so the working directory matters when you use a relative filename.
Step 4: Read the exported CSV
I exported two sample rows with a comma and quotation marks in one note. The file contained this output:
id,name,note 1,Ava,"said ""hello"", then left" 2,Mina,
The comma-containing value is enclosed in quotes, and its internal quotation marks are doubled. The second row’s NULL note is an empty field in this observed output. Check the file with a text editor or a CSV reader before sending it onward.
Step 5: Choose one-query command or output command
Use one-query command when one query should go to a file. Use output command when several query results should keep going to the same destination until you reset it.
.output customers.csv SELECT id, name FROM customers; SELECT id, name FROM former_customers; .output
Calling output command with no filename returns output to the terminal. I checked that reset behavior: after output command returned stdout, the shell still reported current output mode: csv.
The output mode changes how the shell displays query results, not how SQLite stores values. The database remains unchanged by this SELECT. The file contains the returned columns in the order named in the query, which is why an explicit column list is useful when another tool expects a fixed layout.
A CSV export follows the result set, not the source table’s full definition. A query can return a calculated value or renamed column as well as stored fields. That gives you control over what another program receives, but the CSV itself does not preserve the SQL expression or the original column type.
For a table with many fields, writing the required columns by name makes the export clearer to review. It also keeps unrelated fields out of the file. Use SELECT * only when the receiving task needs every column and can handle the table’s current shape.
For example, the query can filter rows before export. Add a WHERE condition to keep only the records the recipient needs, and an ORDER BY clause when the file should have a predictable row order.
SELECT id, name, balance FROM customers WHERE balance > 0 ORDER BY name;
The sqlite3 shell executes the statement as SQL. Enter the dot-commands before the query. A query with no matching rows still runs, but returns no data records.
The one-query output command is a good fit for a one-off report because the redirection applies to the next SQL statement. It then returns subsequent output to the terminal. This limits the chance that a later command or query will be written into the same CSV by mistake.
The output command command is useful for a session that sends several query results to the same file. Its redirection remains active until another destination is selected or output command is called without a filename. Remember that this can combine multiple result sets into one stream, which may not form one consistent CSV table if the queries return different columns.
CSV represents fields as text separated by a delimiter. A field containing a comma or a quotation mark needs quoting so a CSV reader does not mistake that character for a column boundary. In the tested output, the CLI placed quotes around the note and doubled its internal quote characters.
A NULL value and an empty text value can be hard to distinguish in a CSV because both may appear as an empty field. The sample showed the NULL note as an empty position after the final comma. If the receiving workflow needs to preserve that distinction, choose a separate representation in the query, such as a marker string with a CASE expression, and confirm the receiving program expects it.
CSV files do not carry the SQLite column types or table constraints. A spreadsheet may interpret a text field as a number or date when it opens the file. If preserving database types matters, keep the original database or use a format and import procedure that records the schema.
Before sending a large export, test the query without redirection so you can confirm the chosen columns and filter. Then run the same query with one-query command and inspect the first few lines of the file. This catches a wrong database, a misspelled table, or an unexpected header before the file is used elsewhere.
CSV export edge cases to check
Check the database, table name, headers and output path when a CSV does not look right. The sqlite3 shell reports a SQL error if the query refers to a table that is not in the active database.
A missing table stops the query
I ran a query against a table named missing_table and got this error:
Error: in prepare, no such table: missing_table
This means the active database did not contain that table name. Check the filename passed to sqlite3 and inspect available tables with .tables, then correct the SELECT statement before exporting again.
An empty query result may have no data rows
A query that matches no rows has no records to export. If you want column labels even when the result is empty, enable headers and inspect the first line of the file. The SQLite shell’s CSV export example documents a titles option for writing column labels.
SELECT id, name FROM customers WHERE 1 = 0;
Use a narrower SELECT when you need to control the file schema. If another program requires a particular encoding, delimiter or NULL marker, check that program’s import requirements before sharing the export.
Headers describe the columns returned by the query. If a query aliases an expression, its result heading can be different from the source column name. Review the first CSV row before importing the file, especially when a downstream tool maps fields by heading rather than by position.
An empty result has no data records to write. The header option controls whether column labels are included, so the file may still identify its fields even when the query finds no matching rows. This is useful when a receiving process expects a consistent set of columns across runs.
Output filenames are resolved by the shell process. A relative name such as customers.csv goes in the current working directory, which may differ from the directory containing the database file. Use an absolute destination when another process expects a fixed location, and check that directory’s write permission before running the export.
CSV is a text interchange format, so it carries values and field boundaries rather than SQLite’s schema. A consumer may infer a number or date from text that SQLite stored in a different form. Keep the database or document the expected column types when the recipient needs to interpret the values consistently.
Conclusion: export the query you need
The sqlite3 shell can save a query as CSV by combining .mode csv, headers and output redirection. I use one-query command for a single export because it limits file output to one query.
For related shell commands, see the SQLite commands guide. To move in the other direction, read how to import a CSV file into SQLite.
FAQ
These answers cover the shell commands used in the export.
How do I export a SQLite table to CSV?
Open the database with sqlite3, enable .headers on and .mode csv, then use one-query command file.csv before a SELECT query.
How do I export only selected columns?
Name the columns in the SELECT statement, such as SELECT id, name FROM customers;, before directing the output to a CSV file.
What is the difference between one-query command and output command?
one-query command sends one query’s output to a file. output command keeps redirecting output until you call it without a filename to return to the terminal.
