Skip to main content

sql2csv

Pypi Build Status codecov MIT licensed

Run MySQL and PostgreSQL queries and store the results in CSV.

Why sql2csv

sql2csv is a small utility to run MySQL and PostgreSQL queries and store the output in a CSV file.

In some environments like when using MySQL or Aurora in AWS RDS, exporting queries' results to CSV is not available with native tools. sql2csv is a simple module that offers this feature.

Installation

pip3 install sql2csv

# Basic usage
mysql [...] -e "SELECT * FROM table" | sql2csv
# or
psql [...] -c "SELECT * FROM table" | sql2csv

Example

From stdin

For simple queries you can pipe a result directly from mysql or psql to sql2csv.

For more complex queries, it is recommended to use the CLI (see below) to ensure a properly formatted CSV.

mysql -U root -p"secret" my_db -e "SELECT * FROM some_mysql_table;" | sql2csv

id,some_int,some_str,some_date
1,12,hello world,2018-12-01 12:23:12
2,15,hello,2018-12-05 12:18:12
3,18,world,2018-12-08 12:17:12
psql -U postgres my_db -c "SELECT * FROM some_pg_table" | sql2csv

id,some_int,some_str,some_date
1,12,hello world,2018-12-01 12:23:12
2,15,hello,2018-12-05 12:18:12
3,18,world,2018-12-08 12:17:12

Using sql2csv CLI

Output to stdout

$ sql2csv --engine mysql \
  --database my_db --user root --password "secret" \
  --query "SELECT * FROM some_mysql_table"

1,12,hello world,2018-12-01 12:23:12
2,15,hello,2018-12-05 12:18:12
3,18,world,2018-12-08 12:17:12

Output saved in a file

$ sql2csv --engine mysql \
  --database my_db --user root --password "secret" \
  --query "SELECT * FROM some_mysql_table" \
  --headers \
  --out file --destination_file export.csv

# * Exporting rows...
#   ...done
# * The result has been exported to export.csv.

$ cat export.csv 
id,some_int,some_str,some_date
1,12,hello world,2018-12-01 12:23:12
2,15,hello,2018-12-05 12:18:12
3,18,world,2018-12-08 12:17:12

Usage

usage: sql2csv [-h] [-e {mysql,postgresql}] [-H HOST] [-P PORT] -u USER
               [-p PASSWORD] -d DATABASE -q QUERY [-o {stdout,file}]
               [-f DESTINATION_FILE] [-D DELIMITER] [-Q QUOTECHAR] [-t]

optional arguments:
  -h, --help            show this help message and exit
  -e {mysql,postgresql}, --engine {mysql,postgresql}
                        Database engine
  -H HOST, --host HOST  Database host
  -P PORT, --port PORT  Database port
  -u USER, --user USER  Database user
  -p PASSWORD, --password PASSWORD
                        Database password
  -d DATABASE, --database DATABASE
                        Database name
  -q QUERY, --query QUERY
                        SQL query
  -o {stdout,file}, --out {stdout,file}
                        CSV destination
  -f DESTINATION_FILE, --destination_file DESTINATION_FILE
                        CSV destination file
  -D DELIMITER, --delimiter DELIMITER
                        CSV delimiter
  -Q QUOTECHAR, --quotechar QUOTECHAR
                        CSV quote character
  -t, --headers         Include headers

Metadata

Release files for sql2csv 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sql2csv 1.4
File Size Uploaded Provenance Actions
sql2csv-1.4.tar.gz 6.2 kB Without provenance Details for sql2csv-1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sql2csv 1.4
File Interpreter ABI Platform Actions
sql2csv-1.4-py3-none-any.whl Python 3 none any Details for sql2csv-1.4-py3-none-any.whl

Total release size: 12.9 kB

Release files / sql2csv-1.4.tar.gz

Download URL sql2csv-1.4.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2f16c54f2aa7659fafd731786383350d3ad75eb050c9ca958483cb165565066e
BLAKE2b-256 checksum
How to use checksums
00fcf4dc57287100f31a01fffeb31994bbb9dc938621baa066447da12c7df6d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Release files / sql2csv-1.4-py3-none-any.whl

Download URL sql2csv-1.4-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7705b7919d9697f6b724166ba19e57886f4c33b3b40a938b8bed9c5c9cc7f221
BLAKE2b-256 checksum
How to use checksums
b8081e7f9d5434a51b088821616668ec10440c9aab7c57eb52e81d4ce9dc6e38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.0

Security for sql2csv 1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page