1SQL2CSV(1) csvkit SQL2CSV(1)
2
3
4
6 sql2csv - sql2csv Documentation
7
9 Executes arbitrary commands against a SQL database and outputs the re‐
10 sults as a CSV:
11
12 usage: sql2csv [-h] [-v] [-l] [-V] [--db CONNECTION_STRING] [--query QUERY]
13 [-e ENCODING] [-H]
14 [FILE]
15
16 Execute an SQL query on a database and output the result to a CSV file.
17
18 positional arguments:
19 FILE The file to use as the SQL query. If FILE and --query
20 are omitted, the query is piped data via STDIN.
21
22 optional arguments:
23 -h, --help show this help message and exit
24 --db CONNECTION_STRING
25 An sqlalchemy connection string to connect to a
26 database.
27 --query QUERY The SQL query to execute. Overrides FILE and STDIN.
28 -e ENCODING, --encoding ENCODING
29 Specify the encoding of the input query file.
30 -H, --no-header-row Do not output column names.
31
33 Load sample data into a table using csvsql and then query it using
34 sql2csv:
35
36 csvsql --db "sqlite:///dummy.db" --tables "test" --insert examples/dummy.csv
37 sql2csv --db "sqlite:///dummy.db" --query "select * from test"
38
39 Load data about financial aid recipients into PostgreSQL. Then find the
40 three states that received the most, while also filtering out empty
41 rows:
42
43 createdb recipients
44 csvsql --db "postgresql:///recipients" --tables "fy09" --insert examples/realdata/FY09_EDU_Recipients_by_State.csv
45 sql2csv --db "postgresql:///recipients" --query "select * from fy09 where \"State Name\" != '' order by fy09.\"TOTAL\" limit 3"
46
47 You can even use it as a simple SQL calculator (in this example an
48 in-memory SQLite database is used as the default):
49
50 sql2csv --query "select 300 * 47 % 14 * 27 + 7000"
51
52 The connection string accepts parameters. For example, to set the en‐
53 coding of a MySQL database:
54
55 sql2csv --db 'mysql://user:pass@host/database?charset=utf8' --query "SELECT myfield FROM mytable"
56
58 Christopher Groskopf
59
61 2022, Christopher Groskopf
62
63
64
65
661.0.7 Jul 22, 2022 SQL2CSV(1)