sql2csv 1.3__tar.gz → 1.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sql2csv-1.4/LICENSE +21 -0
- sql2csv-1.4/PKG-INFO +144 -0
- {sql2csv-1.3 → sql2csv-1.4}/README.md +3 -3
- sql2csv-1.4/pyproject.toml +43 -0
- sql2csv-1.4/sql2csv.egg-info/PKG-INFO +144 -0
- {sql2csv-1.3 → sql2csv-1.4}/sql2csv.egg-info/SOURCES.txt +2 -1
- {sql2csv-1.3 → sql2csv-1.4}/sql2csv.egg-info/entry_points.txt +0 -1
- sql2csv-1.4/sql2csv.egg-info/requires.txt +2 -0
- {sql2csv-1.3 → sql2csv-1.4}/src/sql2csv.py +10 -7
- sql2csv-1.3/PKG-INFO +0 -155
- sql2csv-1.3/setup.py +0 -40
- sql2csv-1.3/sql2csv.egg-info/PKG-INFO +0 -155
- sql2csv-1.3/sql2csv.egg-info/requires.txt +0 -4
- {sql2csv-1.3 → sql2csv-1.4}/setup.cfg +0 -0
- {sql2csv-1.3 → sql2csv-1.4}/sql2csv.egg-info/dependency_links.txt +0 -0
- {sql2csv-1.3 → sql2csv-1.4}/sql2csv.egg-info/top_level.txt +0 -0
- {sql2csv-1.3 → sql2csv-1.4}/src/__init__.py +0 -0
- {sql2csv-1.3 → sql2csv-1.4}/src/__main__.py +0 -0
sql2csv-1.4/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2017 Gabriel Bordeaux
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
sql2csv-1.4/PKG-INFO
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sql2csv
|
|
3
|
+
Version: 1.4
|
|
4
|
+
Summary: Run MySQL and PostgreSQL queries and store result in CSV
|
|
5
|
+
Author-email: Gabriel Bordeaux <pypi@gab.lc>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/gabfl/sql2csv
|
|
8
|
+
Classifier: Topic :: Database
|
|
9
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
10
|
+
Classifier: Operating System :: MacOS
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Development Status :: 4 - Beta
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: PyMySQL>=1.1
|
|
26
|
+
Requires-Dist: psycopg2-binary>=2.9.10
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# sql2csv
|
|
30
|
+
|
|
31
|
+
[](https://pypi.org/project/sql2csv)
|
|
32
|
+
[](https://github.com/gabfl/sql2csv/actions)
|
|
33
|
+
[](https://codecov.io/gh/gabfl/sql2csv)
|
|
34
|
+
[](https://raw.githubusercontent.com/gabfl/sql2csv/main/LICENSE)
|
|
35
|
+
|
|
36
|
+
Run MySQL and PostgreSQL queries and store the results in CSV.
|
|
37
|
+
|
|
38
|
+
## Why sql2csv
|
|
39
|
+
|
|
40
|
+
`sql2csv` is a small utility to run MySQL and PostgreSQL queries and store the output in a CSV file.
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip3 install sql2csv
|
|
48
|
+
|
|
49
|
+
# Basic usage
|
|
50
|
+
mysql [...] -e "SELECT * FROM table" | sql2csv
|
|
51
|
+
# or
|
|
52
|
+
psql [...] -c "SELECT * FROM table" | sql2csv
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Example
|
|
56
|
+
|
|
57
|
+
### From stdin
|
|
58
|
+
|
|
59
|
+
For simple queries you can pipe a result directly from `mysql` or `psql` to `sql2csv`.
|
|
60
|
+
|
|
61
|
+
For more complex queries, it is recommended to use the CLI (see below) to ensure a properly formatted CSV.
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
mysql -U root -p"secret" my_db -e "SELECT * FROM some_mysql_table;" | sql2csv
|
|
65
|
+
|
|
66
|
+
id,some_int,some_str,some_date
|
|
67
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
68
|
+
2,15,hello,2018-12-05 12:18:12
|
|
69
|
+
3,18,world,2018-12-08 12:17:12
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
psql -U postgres my_db -c "SELECT * FROM some_pg_table" | sql2csv
|
|
74
|
+
|
|
75
|
+
id,some_int,some_str,some_date
|
|
76
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
77
|
+
2,15,hello,2018-12-05 12:18:12
|
|
78
|
+
3,18,world,2018-12-08 12:17:12
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Using `sql2csv` CLI
|
|
82
|
+
|
|
83
|
+
#### Output to stdout
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
$ sql2csv --engine mysql \
|
|
87
|
+
--database my_db --user root --password "secret" \
|
|
88
|
+
--query "SELECT * FROM some_mysql_table"
|
|
89
|
+
|
|
90
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
91
|
+
2,15,hello,2018-12-05 12:18:12
|
|
92
|
+
3,18,world,2018-12-08 12:17:12
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Output saved in a file
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
$ sql2csv --engine mysql \
|
|
99
|
+
--database my_db --user root --password "secret" \
|
|
100
|
+
--query "SELECT * FROM some_mysql_table" \
|
|
101
|
+
--headers \
|
|
102
|
+
--out file --destination_file export.csv
|
|
103
|
+
|
|
104
|
+
# * Exporting rows...
|
|
105
|
+
# ...done
|
|
106
|
+
# * The result has been exported to export.csv.
|
|
107
|
+
|
|
108
|
+
$ cat export.csv
|
|
109
|
+
id,some_int,some_str,some_date
|
|
110
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
111
|
+
2,15,hello,2018-12-05 12:18:12
|
|
112
|
+
3,18,world,2018-12-08 12:17:12
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Usage
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
usage: sql2csv [-h] [-e {mysql,postgresql}] [-H HOST] [-P PORT] -u USER
|
|
119
|
+
[-p PASSWORD] -d DATABASE -q QUERY [-o {stdout,file}]
|
|
120
|
+
[-f DESTINATION_FILE] [-D DELIMITER] [-Q QUOTECHAR] [-t]
|
|
121
|
+
|
|
122
|
+
optional arguments:
|
|
123
|
+
-h, --help show this help message and exit
|
|
124
|
+
-e {mysql,postgresql}, --engine {mysql,postgresql}
|
|
125
|
+
Database engine
|
|
126
|
+
-H HOST, --host HOST Database host
|
|
127
|
+
-P PORT, --port PORT Database port
|
|
128
|
+
-u USER, --user USER Database user
|
|
129
|
+
-p PASSWORD, --password PASSWORD
|
|
130
|
+
Database password
|
|
131
|
+
-d DATABASE, --database DATABASE
|
|
132
|
+
Database name
|
|
133
|
+
-q QUERY, --query QUERY
|
|
134
|
+
SQL query
|
|
135
|
+
-o {stdout,file}, --out {stdout,file}
|
|
136
|
+
CSV destination
|
|
137
|
+
-f DESTINATION_FILE, --destination_file DESTINATION_FILE
|
|
138
|
+
CSV destination file
|
|
139
|
+
-D DELIMITER, --delimiter DELIMITER
|
|
140
|
+
CSV delimiter
|
|
141
|
+
-Q QUOTECHAR, --quotechar QUOTECHAR
|
|
142
|
+
CSV quote character
|
|
143
|
+
-t, --headers Include headers
|
|
144
|
+
```
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
# sql2csv
|
|
2
2
|
|
|
3
3
|
[](https://pypi.org/project/sql2csv)
|
|
4
|
-
[](https://raw.githubusercontent.com/gabfl/sql2csv/
|
|
4
|
+
[](https://github.com/gabfl/sql2csv/actions)
|
|
5
|
+
[](https://codecov.io/gh/gabfl/sql2csv)
|
|
6
|
+
[](https://raw.githubusercontent.com/gabfl/sql2csv/main/LICENSE)
|
|
7
7
|
|
|
8
8
|
Run MySQL and PostgreSQL queries and store the results in CSV.
|
|
9
9
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "sql2csv"
|
|
7
|
+
version = "1.4"
|
|
8
|
+
description = "Run MySQL and PostgreSQL queries and store result in CSV"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
authors = [{ name = "Gabriel Bordeaux", email = "pypi@gab.lc" }]
|
|
13
|
+
requires-python = ">=3.10"
|
|
14
|
+
dependencies = [
|
|
15
|
+
"PyMySQL>=1.1",
|
|
16
|
+
"psycopg2-binary>=2.9.10",
|
|
17
|
+
]
|
|
18
|
+
classifiers = [
|
|
19
|
+
"Topic :: Database",
|
|
20
|
+
"Topic :: Database :: Database Engines/Servers",
|
|
21
|
+
"Operating System :: MacOS",
|
|
22
|
+
"Operating System :: POSIX :: Linux",
|
|
23
|
+
"Natural Language :: English",
|
|
24
|
+
"Programming Language :: Python",
|
|
25
|
+
"Programming Language :: Python :: 3",
|
|
26
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
27
|
+
"Programming Language :: Python :: 3.10",
|
|
28
|
+
"Programming Language :: Python :: 3.11",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Programming Language :: Python :: 3.13",
|
|
31
|
+
"Programming Language :: Python :: 3.14",
|
|
32
|
+
"Development Status :: 4 - Beta",
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.urls]
|
|
36
|
+
Homepage = "https://github.com/gabfl/sql2csv"
|
|
37
|
+
|
|
38
|
+
[project.scripts]
|
|
39
|
+
sql2csv = "sql2csv.sql2csv:main"
|
|
40
|
+
|
|
41
|
+
[tool.setuptools]
|
|
42
|
+
packages = ["sql2csv"]
|
|
43
|
+
package-dir = { "sql2csv" = "src" }
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sql2csv
|
|
3
|
+
Version: 1.4
|
|
4
|
+
Summary: Run MySQL and PostgreSQL queries and store result in CSV
|
|
5
|
+
Author-email: Gabriel Bordeaux <pypi@gab.lc>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/gabfl/sql2csv
|
|
8
|
+
Classifier: Topic :: Database
|
|
9
|
+
Classifier: Topic :: Database :: Database Engines/Servers
|
|
10
|
+
Classifier: Operating System :: MacOS
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Classifier: Natural Language :: English
|
|
13
|
+
Classifier: Programming Language :: Python
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Development Status :: 4 - Beta
|
|
22
|
+
Requires-Python: >=3.10
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: PyMySQL>=1.1
|
|
26
|
+
Requires-Dist: psycopg2-binary>=2.9.10
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# sql2csv
|
|
30
|
+
|
|
31
|
+
[](https://pypi.org/project/sql2csv)
|
|
32
|
+
[](https://github.com/gabfl/sql2csv/actions)
|
|
33
|
+
[](https://codecov.io/gh/gabfl/sql2csv)
|
|
34
|
+
[](https://raw.githubusercontent.com/gabfl/sql2csv/main/LICENSE)
|
|
35
|
+
|
|
36
|
+
Run MySQL and PostgreSQL queries and store the results in CSV.
|
|
37
|
+
|
|
38
|
+
## Why sql2csv
|
|
39
|
+
|
|
40
|
+
`sql2csv` is a small utility to run MySQL and PostgreSQL queries and store the output in a CSV file.
|
|
41
|
+
|
|
42
|
+
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.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip3 install sql2csv
|
|
48
|
+
|
|
49
|
+
# Basic usage
|
|
50
|
+
mysql [...] -e "SELECT * FROM table" | sql2csv
|
|
51
|
+
# or
|
|
52
|
+
psql [...] -c "SELECT * FROM table" | sql2csv
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Example
|
|
56
|
+
|
|
57
|
+
### From stdin
|
|
58
|
+
|
|
59
|
+
For simple queries you can pipe a result directly from `mysql` or `psql` to `sql2csv`.
|
|
60
|
+
|
|
61
|
+
For more complex queries, it is recommended to use the CLI (see below) to ensure a properly formatted CSV.
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
mysql -U root -p"secret" my_db -e "SELECT * FROM some_mysql_table;" | sql2csv
|
|
65
|
+
|
|
66
|
+
id,some_int,some_str,some_date
|
|
67
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
68
|
+
2,15,hello,2018-12-05 12:18:12
|
|
69
|
+
3,18,world,2018-12-08 12:17:12
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
psql -U postgres my_db -c "SELECT * FROM some_pg_table" | sql2csv
|
|
74
|
+
|
|
75
|
+
id,some_int,some_str,some_date
|
|
76
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
77
|
+
2,15,hello,2018-12-05 12:18:12
|
|
78
|
+
3,18,world,2018-12-08 12:17:12
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
### Using `sql2csv` CLI
|
|
82
|
+
|
|
83
|
+
#### Output to stdout
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
$ sql2csv --engine mysql \
|
|
87
|
+
--database my_db --user root --password "secret" \
|
|
88
|
+
--query "SELECT * FROM some_mysql_table"
|
|
89
|
+
|
|
90
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
91
|
+
2,15,hello,2018-12-05 12:18:12
|
|
92
|
+
3,18,world,2018-12-08 12:17:12
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
#### Output saved in a file
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
$ sql2csv --engine mysql \
|
|
99
|
+
--database my_db --user root --password "secret" \
|
|
100
|
+
--query "SELECT * FROM some_mysql_table" \
|
|
101
|
+
--headers \
|
|
102
|
+
--out file --destination_file export.csv
|
|
103
|
+
|
|
104
|
+
# * Exporting rows...
|
|
105
|
+
# ...done
|
|
106
|
+
# * The result has been exported to export.csv.
|
|
107
|
+
|
|
108
|
+
$ cat export.csv
|
|
109
|
+
id,some_int,some_str,some_date
|
|
110
|
+
1,12,hello world,2018-12-01 12:23:12
|
|
111
|
+
2,15,hello,2018-12-05 12:18:12
|
|
112
|
+
3,18,world,2018-12-08 12:17:12
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
## Usage
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
usage: sql2csv [-h] [-e {mysql,postgresql}] [-H HOST] [-P PORT] -u USER
|
|
119
|
+
[-p PASSWORD] -d DATABASE -q QUERY [-o {stdout,file}]
|
|
120
|
+
[-f DESTINATION_FILE] [-D DELIMITER] [-Q QUOTECHAR] [-t]
|
|
121
|
+
|
|
122
|
+
optional arguments:
|
|
123
|
+
-h, --help show this help message and exit
|
|
124
|
+
-e {mysql,postgresql}, --engine {mysql,postgresql}
|
|
125
|
+
Database engine
|
|
126
|
+
-H HOST, --host HOST Database host
|
|
127
|
+
-P PORT, --port PORT Database port
|
|
128
|
+
-u USER, --user USER Database user
|
|
129
|
+
-p PASSWORD, --password PASSWORD
|
|
130
|
+
Database password
|
|
131
|
+
-d DATABASE, --database DATABASE
|
|
132
|
+
Database name
|
|
133
|
+
-q QUERY, --query QUERY
|
|
134
|
+
SQL query
|
|
135
|
+
-o {stdout,file}, --out {stdout,file}
|
|
136
|
+
CSV destination
|
|
137
|
+
-f DESTINATION_FILE, --destination_file DESTINATION_FILE
|
|
138
|
+
CSV destination file
|
|
139
|
+
-D DELIMITER, --delimiter DELIMITER
|
|
140
|
+
CSV delimiter
|
|
141
|
+
-Q QUOTECHAR, --quotechar QUOTECHAR
|
|
142
|
+
CSV quote character
|
|
143
|
+
-t, --headers Include headers
|
|
144
|
+
```
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
import sys
|
|
4
4
|
import csv
|
|
5
5
|
import tempfile
|
|
6
|
+
import os
|
|
6
7
|
from os.path import expanduser
|
|
7
8
|
import json
|
|
8
9
|
|
|
@@ -22,7 +23,7 @@ def get_mysql_connection(host, user, port, password, database):
|
|
|
22
23
|
user=user,
|
|
23
24
|
port=port,
|
|
24
25
|
password=password,
|
|
25
|
-
|
|
26
|
+
database=database,
|
|
26
27
|
charset='utf8mb4',
|
|
27
28
|
client_flag=pymysql.constants.CLIENT.MULTI_STATEMENTS
|
|
28
29
|
)
|
|
@@ -118,7 +119,7 @@ def stringify_items(row):
|
|
|
118
119
|
|
|
119
120
|
row = list(row)
|
|
120
121
|
for k, item in enumerate(row):
|
|
121
|
-
if(isinstance(item, dict)):
|
|
122
|
+
if (isinstance(item, dict)):
|
|
122
123
|
row[k] = json.dumps(item)
|
|
123
124
|
|
|
124
125
|
return tuple(row)
|
|
@@ -173,12 +174,16 @@ def get_writer(file_, delimiter=',', quotechar='"'):
|
|
|
173
174
|
)
|
|
174
175
|
|
|
175
176
|
|
|
176
|
-
def file_to_stdout():
|
|
177
|
+
def file_to_stdout(remove=False):
|
|
177
178
|
""" Print file content to stdout """
|
|
178
179
|
|
|
179
180
|
with open(file_.name) as f:
|
|
180
181
|
print(f.read())
|
|
181
182
|
|
|
183
|
+
# Remove the temporary file once it has been printed
|
|
184
|
+
if remove:
|
|
185
|
+
os.remove(file_.name)
|
|
186
|
+
|
|
182
187
|
|
|
183
188
|
def stdin_to_csv(delimiter=',', quotechar='"'):
|
|
184
189
|
""" Parse stdin and return output in a CSV format """
|
|
@@ -190,8 +195,6 @@ def stdin_to_csv(delimiter=',', quotechar='"'):
|
|
|
190
195
|
# Parse lines and add to file
|
|
191
196
|
separator = None
|
|
192
197
|
for line in sys.stdin:
|
|
193
|
-
# Strip whitespaces
|
|
194
|
-
line.strip()
|
|
195
198
|
|
|
196
199
|
if not discard_line(line):
|
|
197
200
|
# Get column separator
|
|
@@ -211,7 +214,7 @@ def stdin_to_csv(delimiter=',', quotechar='"'):
|
|
|
211
214
|
# Write row
|
|
212
215
|
writer.writerow(row)
|
|
213
216
|
|
|
214
|
-
file_to_stdout()
|
|
217
|
+
file_to_stdout(remove=True)
|
|
215
218
|
|
|
216
219
|
|
|
217
220
|
def query_to_csv(engine, host, user, port, password, database, query, headers=False, out_type='stdout', destination_file=None, delimiter=',', quotechar='"', print_info=1000):
|
|
@@ -261,7 +264,7 @@ def query_to_csv(engine, host, user, port, password, database, query, headers=Fa
|
|
|
261
264
|
|
|
262
265
|
# Print stdout
|
|
263
266
|
if out_type == 'stdout':
|
|
264
|
-
file_to_stdout()
|
|
267
|
+
file_to_stdout(remove=True)
|
|
265
268
|
|
|
266
269
|
|
|
267
270
|
def main():
|
sql2csv-1.3/PKG-INFO
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.1
|
|
2
|
-
Name: sql2csv
|
|
3
|
-
Version: 1.3
|
|
4
|
-
Summary: Run MySQL and PostgreSQL queries and store result in CSV
|
|
5
|
-
Home-page: https://github.com/gabfl/sql2csv
|
|
6
|
-
Author: Gabriel Bordeaux
|
|
7
|
-
Author-email: pypi@gab.lc
|
|
8
|
-
License: MIT
|
|
9
|
-
Description: sql2csv
|
|
10
|
-
=======
|
|
11
|
-
|
|
12
|
-
|Pypi| |Build Status| |codecov| |MIT licensed|
|
|
13
|
-
|
|
14
|
-
Run MySQL and PostgreSQL queries and store the results in CSV.
|
|
15
|
-
|
|
16
|
-
Why sql2csv
|
|
17
|
-
-----------
|
|
18
|
-
|
|
19
|
-
``sql2csv`` is a small utility to run MySQL and PostgreSQL queries and
|
|
20
|
-
store the output in a CSV file.
|
|
21
|
-
|
|
22
|
-
In some environments like when using MySQL or Aurora in AWS RDS,
|
|
23
|
-
exporting queries’ results to CSV is not available with native tools.
|
|
24
|
-
``sql2csv`` is a simple module that offers this feature.
|
|
25
|
-
|
|
26
|
-
Installation
|
|
27
|
-
------------
|
|
28
|
-
|
|
29
|
-
.. code:: bash
|
|
30
|
-
|
|
31
|
-
pip3 install sql2csv
|
|
32
|
-
|
|
33
|
-
# Basic usage
|
|
34
|
-
mysql [...] -e "SELECT * FROM table" | sql2csv
|
|
35
|
-
# or
|
|
36
|
-
psql [...] -c "SELECT * FROM table" | sql2csv
|
|
37
|
-
|
|
38
|
-
Example
|
|
39
|
-
-------
|
|
40
|
-
|
|
41
|
-
From stdin
|
|
42
|
-
~~~~~~~~~~
|
|
43
|
-
|
|
44
|
-
For simple queries you can pipe a result directly from ``mysql`` or
|
|
45
|
-
``psql`` to ``sql2csv``.
|
|
46
|
-
|
|
47
|
-
For more complex queries, it is recommended to use the CLI (see below)
|
|
48
|
-
to ensure a properly formatted CSV.
|
|
49
|
-
|
|
50
|
-
.. code:: bash
|
|
51
|
-
|
|
52
|
-
mysql -U root -p"secret" my_db -e "SELECT * FROM some_mysql_table;" | sql2csv
|
|
53
|
-
|
|
54
|
-
id,some_int,some_str,some_date
|
|
55
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
56
|
-
2,15,hello,2018-12-05 12:18:12
|
|
57
|
-
3,18,world,2018-12-08 12:17:12
|
|
58
|
-
|
|
59
|
-
.. code:: bash
|
|
60
|
-
|
|
61
|
-
psql -U postgres my_db -c "SELECT * FROM some_pg_table" | sql2csv
|
|
62
|
-
|
|
63
|
-
id,some_int,some_str,some_date
|
|
64
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
65
|
-
2,15,hello,2018-12-05 12:18:12
|
|
66
|
-
3,18,world,2018-12-08 12:17:12
|
|
67
|
-
|
|
68
|
-
Using ``sql2csv`` CLI
|
|
69
|
-
~~~~~~~~~~~~~~~~~~~~~
|
|
70
|
-
|
|
71
|
-
Output to stdout
|
|
72
|
-
^^^^^^^^^^^^^^^^
|
|
73
|
-
|
|
74
|
-
.. code:: bash
|
|
75
|
-
|
|
76
|
-
$ sql2csv --engine mysql \
|
|
77
|
-
--database my_db --user root --password "secret" \
|
|
78
|
-
--query "SELECT * FROM some_mysql_table"
|
|
79
|
-
|
|
80
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
81
|
-
2,15,hello,2018-12-05 12:18:12
|
|
82
|
-
3,18,world,2018-12-08 12:17:12
|
|
83
|
-
|
|
84
|
-
Output saved in a file
|
|
85
|
-
^^^^^^^^^^^^^^^^^^^^^^
|
|
86
|
-
|
|
87
|
-
.. code:: bash
|
|
88
|
-
|
|
89
|
-
$ sql2csv --engine mysql \
|
|
90
|
-
--database my_db --user root --password "secret" \
|
|
91
|
-
--query "SELECT * FROM some_mysql_table" \
|
|
92
|
-
--headers \
|
|
93
|
-
--out file --destination_file export.csv
|
|
94
|
-
|
|
95
|
-
# * Exporting rows...
|
|
96
|
-
# ...done
|
|
97
|
-
# * The result has been exported to export.csv.
|
|
98
|
-
|
|
99
|
-
$ cat export.csv
|
|
100
|
-
id,some_int,some_str,some_date
|
|
101
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
102
|
-
2,15,hello,2018-12-05 12:18:12
|
|
103
|
-
3,18,world,2018-12-08 12:17:12
|
|
104
|
-
|
|
105
|
-
Usage
|
|
106
|
-
-----
|
|
107
|
-
|
|
108
|
-
.. code:: bash
|
|
109
|
-
|
|
110
|
-
usage: sql2csv [-h] [-e {mysql,postgresql}] [-H HOST] [-P PORT] -u USER
|
|
111
|
-
[-p PASSWORD] -d DATABASE -q QUERY [-o {stdout,file}]
|
|
112
|
-
[-f DESTINATION_FILE] [-D DELIMITER] [-Q QUOTECHAR] [-t]
|
|
113
|
-
|
|
114
|
-
optional arguments:
|
|
115
|
-
-h, --help show this help message and exit
|
|
116
|
-
-e {mysql,postgresql}, --engine {mysql,postgresql}
|
|
117
|
-
Database engine
|
|
118
|
-
-H HOST, --host HOST Database host
|
|
119
|
-
-P PORT, --port PORT Database port
|
|
120
|
-
-u USER, --user USER Database user
|
|
121
|
-
-p PASSWORD, --password PASSWORD
|
|
122
|
-
Database password
|
|
123
|
-
-d DATABASE, --database DATABASE
|
|
124
|
-
Database name
|
|
125
|
-
-q QUERY, --query QUERY
|
|
126
|
-
SQL query
|
|
127
|
-
-o {stdout,file}, --out {stdout,file}
|
|
128
|
-
CSV destination
|
|
129
|
-
-f DESTINATION_FILE, --destination_file DESTINATION_FILE
|
|
130
|
-
CSV destination file
|
|
131
|
-
-D DELIMITER, --delimiter DELIMITER
|
|
132
|
-
CSV delimiter
|
|
133
|
-
-Q QUOTECHAR, --quotechar QUOTECHAR
|
|
134
|
-
CSV quote character
|
|
135
|
-
-t, --headers Include headers
|
|
136
|
-
|
|
137
|
-
.. |Pypi| image:: https://img.shields.io/pypi/v/sql2csv.svg
|
|
138
|
-
:target: https://pypi.org/project/sql2csv
|
|
139
|
-
.. |Build Status| image:: https://travis-ci.org/gabfl/sql2csv.svg?branch=master
|
|
140
|
-
:target: https://travis-ci.org/gabfl/sql2csv
|
|
141
|
-
.. |codecov| image:: https://codecov.io/gh/gabfl/sql2csv/branch/master/graph/badge.svg
|
|
142
|
-
:target: https://codecov.io/gh/gabfl/sql2csv
|
|
143
|
-
.. |MIT licensed| image:: https://img.shields.io/badge/license-MIT-green.svg
|
|
144
|
-
:target: https://raw.githubusercontent.com/gabfl/sql2csv/master/LICENSE
|
|
145
|
-
|
|
146
|
-
Platform: UNKNOWN
|
|
147
|
-
Classifier: Topic :: Database
|
|
148
|
-
Classifier: Topic :: Database :: Database Engines/Servers
|
|
149
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
150
|
-
Classifier: Operating System :: MacOS
|
|
151
|
-
Classifier: Operating System :: POSIX :: Linux
|
|
152
|
-
Classifier: Natural Language :: English
|
|
153
|
-
Classifier: Programming Language :: Python :: 3
|
|
154
|
-
Classifier: Programming Language :: Python
|
|
155
|
-
Classifier: Development Status :: 4 - Beta
|
sql2csv-1.3/setup.py
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
from setuptools import setup
|
|
2
|
-
|
|
3
|
-
try:
|
|
4
|
-
import pypandoc
|
|
5
|
-
long_description = pypandoc.convert('README.md', 'rst')
|
|
6
|
-
except(IOError, ImportError):
|
|
7
|
-
long_description = open('README.md').read()
|
|
8
|
-
|
|
9
|
-
setup(
|
|
10
|
-
name='sql2csv',
|
|
11
|
-
version='1.3',
|
|
12
|
-
description='Run MySQL and PostgreSQL queries and store result in CSV',
|
|
13
|
-
long_description=long_description,
|
|
14
|
-
author='Gabriel Bordeaux',
|
|
15
|
-
author_email='pypi@gab.lc',
|
|
16
|
-
url='https://github.com/gabfl/sql2csv',
|
|
17
|
-
license='MIT',
|
|
18
|
-
packages=['sql2csv'],
|
|
19
|
-
package_dir={'sql2csv': 'src'},
|
|
20
|
-
install_requires=['argparse', 'PyYAML', 'pymysql',
|
|
21
|
-
'psycopg2-binary'], # external dependencies
|
|
22
|
-
entry_points={
|
|
23
|
-
'console_scripts': [
|
|
24
|
-
'sql2csv = sql2csv.sql2csv:main',
|
|
25
|
-
],
|
|
26
|
-
},
|
|
27
|
-
classifiers=[ # see https://pypi.python.org/pypi?%3Aaction=list_classifiers
|
|
28
|
-
'Topic :: Database',
|
|
29
|
-
'Topic :: Database :: Database Engines/Servers',
|
|
30
|
-
'License :: OSI Approved :: MIT License',
|
|
31
|
-
'Operating System :: MacOS',
|
|
32
|
-
'Operating System :: POSIX :: Linux',
|
|
33
|
-
'Natural Language :: English',
|
|
34
|
-
# 'Programming Language :: Python :: 2.7',
|
|
35
|
-
'Programming Language :: Python :: 3',
|
|
36
|
-
'Programming Language :: Python',
|
|
37
|
-
'Development Status :: 4 - Beta',
|
|
38
|
-
# 'Development Status :: 5 - Production/Stable',
|
|
39
|
-
],
|
|
40
|
-
)
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 1.1
|
|
2
|
-
Name: sql2csv
|
|
3
|
-
Version: 1.3
|
|
4
|
-
Summary: Run MySQL and PostgreSQL queries and store result in CSV
|
|
5
|
-
Home-page: https://github.com/gabfl/sql2csv
|
|
6
|
-
Author: Gabriel Bordeaux
|
|
7
|
-
Author-email: pypi@gab.lc
|
|
8
|
-
License: MIT
|
|
9
|
-
Description: sql2csv
|
|
10
|
-
=======
|
|
11
|
-
|
|
12
|
-
|Pypi| |Build Status| |codecov| |MIT licensed|
|
|
13
|
-
|
|
14
|
-
Run MySQL and PostgreSQL queries and store the results in CSV.
|
|
15
|
-
|
|
16
|
-
Why sql2csv
|
|
17
|
-
-----------
|
|
18
|
-
|
|
19
|
-
``sql2csv`` is a small utility to run MySQL and PostgreSQL queries and
|
|
20
|
-
store the output in a CSV file.
|
|
21
|
-
|
|
22
|
-
In some environments like when using MySQL or Aurora in AWS RDS,
|
|
23
|
-
exporting queries’ results to CSV is not available with native tools.
|
|
24
|
-
``sql2csv`` is a simple module that offers this feature.
|
|
25
|
-
|
|
26
|
-
Installation
|
|
27
|
-
------------
|
|
28
|
-
|
|
29
|
-
.. code:: bash
|
|
30
|
-
|
|
31
|
-
pip3 install sql2csv
|
|
32
|
-
|
|
33
|
-
# Basic usage
|
|
34
|
-
mysql [...] -e "SELECT * FROM table" | sql2csv
|
|
35
|
-
# or
|
|
36
|
-
psql [...] -c "SELECT * FROM table" | sql2csv
|
|
37
|
-
|
|
38
|
-
Example
|
|
39
|
-
-------
|
|
40
|
-
|
|
41
|
-
From stdin
|
|
42
|
-
~~~~~~~~~~
|
|
43
|
-
|
|
44
|
-
For simple queries you can pipe a result directly from ``mysql`` or
|
|
45
|
-
``psql`` to ``sql2csv``.
|
|
46
|
-
|
|
47
|
-
For more complex queries, it is recommended to use the CLI (see below)
|
|
48
|
-
to ensure a properly formatted CSV.
|
|
49
|
-
|
|
50
|
-
.. code:: bash
|
|
51
|
-
|
|
52
|
-
mysql -U root -p"secret" my_db -e "SELECT * FROM some_mysql_table;" | sql2csv
|
|
53
|
-
|
|
54
|
-
id,some_int,some_str,some_date
|
|
55
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
56
|
-
2,15,hello,2018-12-05 12:18:12
|
|
57
|
-
3,18,world,2018-12-08 12:17:12
|
|
58
|
-
|
|
59
|
-
.. code:: bash
|
|
60
|
-
|
|
61
|
-
psql -U postgres my_db -c "SELECT * FROM some_pg_table" | sql2csv
|
|
62
|
-
|
|
63
|
-
id,some_int,some_str,some_date
|
|
64
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
65
|
-
2,15,hello,2018-12-05 12:18:12
|
|
66
|
-
3,18,world,2018-12-08 12:17:12
|
|
67
|
-
|
|
68
|
-
Using ``sql2csv`` CLI
|
|
69
|
-
~~~~~~~~~~~~~~~~~~~~~
|
|
70
|
-
|
|
71
|
-
Output to stdout
|
|
72
|
-
^^^^^^^^^^^^^^^^
|
|
73
|
-
|
|
74
|
-
.. code:: bash
|
|
75
|
-
|
|
76
|
-
$ sql2csv --engine mysql \
|
|
77
|
-
--database my_db --user root --password "secret" \
|
|
78
|
-
--query "SELECT * FROM some_mysql_table"
|
|
79
|
-
|
|
80
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
81
|
-
2,15,hello,2018-12-05 12:18:12
|
|
82
|
-
3,18,world,2018-12-08 12:17:12
|
|
83
|
-
|
|
84
|
-
Output saved in a file
|
|
85
|
-
^^^^^^^^^^^^^^^^^^^^^^
|
|
86
|
-
|
|
87
|
-
.. code:: bash
|
|
88
|
-
|
|
89
|
-
$ sql2csv --engine mysql \
|
|
90
|
-
--database my_db --user root --password "secret" \
|
|
91
|
-
--query "SELECT * FROM some_mysql_table" \
|
|
92
|
-
--headers \
|
|
93
|
-
--out file --destination_file export.csv
|
|
94
|
-
|
|
95
|
-
# * Exporting rows...
|
|
96
|
-
# ...done
|
|
97
|
-
# * The result has been exported to export.csv.
|
|
98
|
-
|
|
99
|
-
$ cat export.csv
|
|
100
|
-
id,some_int,some_str,some_date
|
|
101
|
-
1,12,hello world,2018-12-01 12:23:12
|
|
102
|
-
2,15,hello,2018-12-05 12:18:12
|
|
103
|
-
3,18,world,2018-12-08 12:17:12
|
|
104
|
-
|
|
105
|
-
Usage
|
|
106
|
-
-----
|
|
107
|
-
|
|
108
|
-
.. code:: bash
|
|
109
|
-
|
|
110
|
-
usage: sql2csv [-h] [-e {mysql,postgresql}] [-H HOST] [-P PORT] -u USER
|
|
111
|
-
[-p PASSWORD] -d DATABASE -q QUERY [-o {stdout,file}]
|
|
112
|
-
[-f DESTINATION_FILE] [-D DELIMITER] [-Q QUOTECHAR] [-t]
|
|
113
|
-
|
|
114
|
-
optional arguments:
|
|
115
|
-
-h, --help show this help message and exit
|
|
116
|
-
-e {mysql,postgresql}, --engine {mysql,postgresql}
|
|
117
|
-
Database engine
|
|
118
|
-
-H HOST, --host HOST Database host
|
|
119
|
-
-P PORT, --port PORT Database port
|
|
120
|
-
-u USER, --user USER Database user
|
|
121
|
-
-p PASSWORD, --password PASSWORD
|
|
122
|
-
Database password
|
|
123
|
-
-d DATABASE, --database DATABASE
|
|
124
|
-
Database name
|
|
125
|
-
-q QUERY, --query QUERY
|
|
126
|
-
SQL query
|
|
127
|
-
-o {stdout,file}, --out {stdout,file}
|
|
128
|
-
CSV destination
|
|
129
|
-
-f DESTINATION_FILE, --destination_file DESTINATION_FILE
|
|
130
|
-
CSV destination file
|
|
131
|
-
-D DELIMITER, --delimiter DELIMITER
|
|
132
|
-
CSV delimiter
|
|
133
|
-
-Q QUOTECHAR, --quotechar QUOTECHAR
|
|
134
|
-
CSV quote character
|
|
135
|
-
-t, --headers Include headers
|
|
136
|
-
|
|
137
|
-
.. |Pypi| image:: https://img.shields.io/pypi/v/sql2csv.svg
|
|
138
|
-
:target: https://pypi.org/project/sql2csv
|
|
139
|
-
.. |Build Status| image:: https://travis-ci.org/gabfl/sql2csv.svg?branch=master
|
|
140
|
-
:target: https://travis-ci.org/gabfl/sql2csv
|
|
141
|
-
.. |codecov| image:: https://codecov.io/gh/gabfl/sql2csv/branch/master/graph/badge.svg
|
|
142
|
-
:target: https://codecov.io/gh/gabfl/sql2csv
|
|
143
|
-
.. |MIT licensed| image:: https://img.shields.io/badge/license-MIT-green.svg
|
|
144
|
-
:target: https://raw.githubusercontent.com/gabfl/sql2csv/master/LICENSE
|
|
145
|
-
|
|
146
|
-
Platform: UNKNOWN
|
|
147
|
-
Classifier: Topic :: Database
|
|
148
|
-
Classifier: Topic :: Database :: Database Engines/Servers
|
|
149
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
150
|
-
Classifier: Operating System :: MacOS
|
|
151
|
-
Classifier: Operating System :: POSIX :: Linux
|
|
152
|
-
Classifier: Natural Language :: English
|
|
153
|
-
Classifier: Programming Language :: Python :: 3
|
|
154
|
-
Classifier: Programming Language :: Python
|
|
155
|
-
Classifier: Development Status :: 4 - Beta
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|