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 SQL query. If both FILE and QUERY
20 are omitted, query will be read from 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. If specified, it overrides
28 FILE and STDIN.
29 -e ENCODING, --encoding ENCODING
30 Specify the encoding of the input query file.
31 -H, --no-header-row Do not output column names.
32
34 Load sample data into a table using csvsql and then query it using
35 sql2csv:
36
37 csvsql --db "sqlite:///dummy.db" --tables "test" --insert examples/dummy.csv
38 sql2csv --db "sqlite:///dummy.db" --query "select * from test"
39
40 Load data about financial aid recipients into PostgreSQL. Then find the
41 three states that received the most, while also filtering out empty
42 rows:
43
44 createdb recipients
45 csvsql --db "postgresql:///recipients" --tables "fy09" --insert examples/realdata/FY09_EDU_Recipients_by_State.csv
46 sql2csv --db "postgresql:///recipients" --query "select * from fy09 where \"State Name\" != '' order by fy09.\"TOTAL\" limit 3"
47
48 You can even use it as a simple SQL calculator (in this example an
49 in-memory SQLite database is used as the default):
50
51 sql2csv --query "select 300 * 47 % 14 * 27 + 7000"
52
53 The connection string accepts parameters. For example, to set the en‐
54 coding of a MySQL database:
55
56 sql2csv --db 'mysql://user:pass@host/database?charset=utf8' --query "SELECT myfield FROM mytable"
57
59 Christopher Groskopf
60
62 2022, Christopher Groskopf
63
64
65
66
671.0.4 Jan 21, 2022 SQL2CSV(1)