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 both FILE and --query are 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's create_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's execution_options(), also a space-separated pair. Also repeatable, bestie. For example: stream_results True
  • --query QUERY β€” the SQL query to actually execute. This one overrides FILE and 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 🫢


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