Run MySQL and PostgreSQL queries and store result in CSV
Project description
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
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file sql2csv-1.3.tar.gz
.
File metadata
- Download URL: sql2csv-1.3.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbc8e3dacebe466d58da496192fca2a3bfb0de61357692a2ebf65eb5c51c9ef4 |
|
MD5 | 9766d1f0af6172506b81db91718c249d |
|
BLAKE2b-256 | fd878dfba8ad95658f6d335394711d86cea81854a10168bab57bb7e93e1ffe74 |
File details
Details for the file sql2csv-1.3-py2.py3-none-any.whl
.
File metadata
- Download URL: sql2csv-1.3-py2.py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3f900a45f90491cf98bece1a61603724be4b55ea70e5438da940805c08de602 |
|
MD5 | 52a8e008825b731919d6c73f842706f6 |
|
BLAKE2b-256 | ddf531c860129ac9a65aaf97892878d2de7eb7180c479d90afff155192f591da |