SQL2CSV(1)
β¨ NAME β¨
sql2csv β okay bestie, this is the sql2csv documentation, and it's giving query goes in, CSV comes out π
π¦ DESCRIPTION π¦
So here's the tea: sql2csv runs arbitrary commands against a SQL database and spits the results back out as a CSV. That's it. That's the whole vibe. β¨
Check the usage:
usage: sql2csv [-h] [-v] [-l] [-V] [--db CONNECTION_STRING] [--query QUERY]
[-e ENCODING] [-H]
[FILE]
Execute a SQL query on a database and output the result to a CSV file.
πΈ positional arguments
FILEβ the file you wanna use as the SQL query. And listen, if bothFILEand--queryare missing, then the query gets piped in via STDIN instead. She's flexible like that π«Ά
π optional arguments
-h,--helpβ shows this help message and then dips. iconic.--db CONNECTION_STRINGβ a SQLAlchemy connection string so you can actually connect to a database π--engine-option ENGINE_OPTION ENGINE_OPTIONβ a keyword argument for SQLAlchemy'screate_engine(), written as a space-separated pair. You can drop this one multiple times, no cap. For example:thick_mode True--execution-option EXECUTION_OPTION EXECUTION_OPTIONβ a keyword argument for SQLAlchemy'sexecution_options(), also a space-separated pair. Also repeatable, bestie. For example:stream_results True--query QUERYβ the SQL query to actually execute. This one overridesFILEand STDIN, she's the main character π-e ENCODING,--encoding ENCODINGβ specify the encoding of the input query file π-H,--no-header-rowβ do not output the column names. headerless era π
π EXAMPLES π
π Example one: sample data moment
Load sample data into a table using csvsql, then query it with sql2csv:
csvsql --db "sqlite:///dummy.db" --tables "test" --insert examples/dummy.csv
sql2csv --db "sqlite:///dummy.db" --query "select * from test"
π Example two: financial aid glow-up
Load data about financial aid recipients into PostgreSQL. Then find the three states that received the most, while also filtering out empty rows:
createdb recipients
csvsql --db "postgresql:///recipients" --tables "fy09" --insert examples/realdata/FY09_EDU_Recipients_by_State.csv
sql2csv --db "postgresql:///recipients" --query "select * from fy09 where \"State Name\" != '' order by fy09.\"TOTAL\" limit 3"
π§ Example three: calculator era
You can even use it as a simple SQL calculator β in this example an in-memory SQLite database is used as the default. literally math but make it SQL π₯
sql2csv --query "select 300 * 47 % 14 * 27 + 7000"
π Example four: connection string with params
The connection string accepts parameters. For example, to set the encoding of a MySQL database:
sql2csv --db 'mysql://user:pass@host/database?charset=utf8' --query "SELECT myfield FROM mytable"
π AUTHOR π
Christopher Groskopf and contributors π«Ά
β COPYRIGHT β
2016, Christopher Groskopf and James McKinney
and that's the whole page, bestie π version 2.2.0, dated Aug 16, 2024 β go forth and CSV-ify everything, iykyk β¨
π¦ where this page comes from
- arch rolling: csvkit 2.2.0-2 Β· MIT
- nixos 26.05: csvkit 2.2.0
- nixpkgs unstable: csvkit 2.2.0
- ubuntu resolute: csvkit 2.2.0-1 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms