|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + <title>Caltech Library's Digital Library Development Sandbox</title> |
| 5 | + <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'> |
| 6 | + <link rel="stylesheet" href="/css/site.css"> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | +<header> |
| 10 | +<a href="http://library.caltech.edu"><img src="/assets/liblogo.gif" alt="Caltech Library logo"></a> |
| 11 | +</header> |
| 12 | +<nav> |
| 13 | +<ul> |
| 14 | +<li> |
| 15 | +<a href="/">Home</a> |
| 16 | +</li> |
| 17 | +<li> |
| 18 | +<a href="index.html">README</a> |
| 19 | +</li> |
| 20 | +<li> |
| 21 | +<a href="license.html">LICENSE</a> |
| 22 | +</li> |
| 23 | +<li> |
| 24 | +<a href="install.html">INSTALL</a> |
| 25 | +</li> |
| 26 | +<li> |
| 27 | +<a href="docs/">Documentation</a> |
| 28 | +</li> |
| 29 | +<li> |
| 30 | +<a href="how-to/">How To</a> |
| 31 | +</li> |
| 32 | +<li> |
| 33 | +<a href="about.html">About</a> |
| 34 | +</li> |
| 35 | +<li> |
| 36 | +<a |
| 37 | +href="https://github.com/caltechlibrary/datatools">Github</a> |
| 38 | +</li> |
| 39 | +</ul> |
| 40 | +</nav> |
| 41 | + |
| 42 | +<section> |
| 43 | +<h1 id="name"> |
| 44 | +NAME |
| 45 | +</h1> |
| 46 | +<p> |
| 47 | +sql2csv |
| 48 | +</p> |
| 49 | +<h1 id="synopsis"> |
| 50 | +SYNOPSIS |
| 51 | +</h1> |
| 52 | +<p> |
| 53 | +sql2csv <a href="#options">OPTIONS</a> SQL_STATEMENT |
| 54 | +</p> |
| 55 | +<p> |
| 56 | +sql2csv <a href="#options">OPTIONS</a> CONFIG_FILE SQL_STATEMENT |
| 57 | +</p> |
| 58 | +<h1 id="description"> |
| 59 | +DESCRIPTION |
| 60 | +</h1> |
| 61 | +<p> |
| 62 | +sql2csv takes a config file describing a SQL database connection and |
| 63 | +output options needed and a SQL statement as the final parameter. The |
| 64 | +output of the SQL query is rendered in CSV format to standard out. |
| 65 | +sql2csv supports querying MySQL 8, Postgres and SQLite3 databases. |
| 66 | +</p> |
| 67 | +<p> |
| 68 | +The configuration file is a JSON document with the following key value |
| 69 | +pairs. |
| 70 | +</p> |
| 71 | +<dl> |
| 72 | +<dt> |
| 73 | +dsn_url |
| 74 | +</dt> |
| 75 | +<dd> |
| 76 | +(string) A data source name in URL form where the “protocol” element |
| 77 | +identifies the database resource being accessed (e.g. “sqlite://”, |
| 78 | +“mysql://”, “postgres://”). A data source name are rescribed at <a |
| 79 | +href="https://github.com/golang/go/wiki/SQLInterface" |
| 80 | +class="uri">https://github.com/golang/go/wiki/SQLInterface</a>. |
| 81 | +</dd> |
| 82 | +<dt> |
| 83 | +header_row |
| 84 | +</dt> |
| 85 | +<dd> |
| 86 | +(boolean) if true print a header row in the output, false for no header |
| 87 | +row output |
| 88 | +</dd> |
| 89 | +<dt> |
| 90 | +delimiter |
| 91 | +</dt> |
| 92 | +<dd> |
| 93 | +(single character, default is “,”), sets the field delimited used in |
| 94 | +output. It can be set to “ for tab separated values. |
| 95 | +</dd> |
| 96 | +<dt> |
| 97 | +use_crlf |
| 98 | +</dt> |
| 99 | +<dd> |
| 100 | +(boolean, default is false) if set to true to use “” as the line |
| 101 | +terminator between rows of output. |
| 102 | +</dd> |
| 103 | +</dl> |
| 104 | +<p> |
| 105 | +To connect with a database sql2csv relies on a data source name (DSN) in |
| 106 | +URL format. In the URL form the URL’s scheme indicates the type of |
| 107 | +database you are connecting to (e.g. sqlite, mysql, postgres). The rest |
| 108 | +of the DNS has the following form |
| 109 | +</p> |
| 110 | +<pre><code>[username[:password]@][protocol[(address)]]/dbname[?param1=value1&...&paramN=valueN]</code></pre> |
| 111 | +<p> |
| 112 | +For a simple database like SQLite3 a minimal DSN in url form for a |
| 113 | +database file “my_database.sqlite3” would look like |
| 114 | +</p> |
| 115 | +<pre><code> sqlite://file:my_database.sqlite3</code></pre> |
| 116 | +<p> |
| 117 | +For MySQL you need to provide more information to connect |
| 118 | +(e.g. username, password). In this example the username is “jane.doe”, |
| 119 | +password is “something_secret” the database is “my_database”. (this |
| 120 | +example assumes that MySQL 8 is running on localhost at the usual port). |
| 121 | +</p> |
| 122 | +<pre><code> mysql://jane.doe:something_secret@/my_database</code></pre> |
| 123 | +<p> |
| 124 | +Postgres is similar to the MySQL connection string except the “scheme” |
| 125 | +is “postgres” instead of “mysql”. |
| 126 | +</p> |
| 127 | +<h1 id="options"> |
| 128 | +OPTIONS |
| 129 | +</h1> |
| 130 | +<dl> |
| 131 | +<dt> |
| 132 | +-help |
| 133 | +</dt> |
| 134 | +<dd> |
| 135 | +display help |
| 136 | +</dd> |
| 137 | +<dt> |
| 138 | +-version |
| 139 | +</dt> |
| 140 | +<dd> |
| 141 | +display version |
| 142 | +</dd> |
| 143 | +<dt> |
| 144 | +-license |
| 145 | +</dt> |
| 146 | +<dd> |
| 147 | +display license |
| 148 | +</dd> |
| 149 | +</dl> |
| 150 | +<p> |
| 151 | +A the following options will override a configuration. |
| 152 | +</p> |
| 153 | +<dl> |
| 154 | +<dt> |
| 155 | +-dsn |
| 156 | +</dt> |
| 157 | +<dd> |
| 158 | +use the data source name in URL form instead of a JSON configuration |
| 159 | +file |
| 160 | +</dd> |
| 161 | +<dt> |
| 162 | +-header |
| 163 | +</dt> |
| 164 | +<dd> |
| 165 | +use a header row if true, false skip the header row |
| 166 | +</dd> |
| 167 | +<dt> |
| 168 | +-delimiter |
| 169 | +</dt> |
| 170 | +<dd> |
| 171 | +Set the delimiter to use, default is comma |
| 172 | +</dd> |
| 173 | +<dt> |
| 174 | +-use-cdlf |
| 175 | +</dt> |
| 176 | +<dd> |
| 177 | +Force the line ending per row to carage return and line feed if true, |
| 178 | +false use line feed |
| 179 | +</dd> |
| 180 | +</dl> |
| 181 | +<h1 id="example"> |
| 182 | +EXAMPLE |
| 183 | +</h1> |
| 184 | +<p> |
| 185 | +Using the “dbcfg.json” configuration file, display ten rows from table |
| 186 | +“mytable” in database indicated in “dbcfg.json”. |
| 187 | +</p> |
| 188 | +<p> |
| 189 | +sql2csv dbcfg.json ‘SELECT * FROM mytable LIMIT 10’ |
| 190 | +</p> |
| 191 | +<p> |
| 192 | +The CSV output is written standard out and can be redirected into a file |
| 193 | +if desired. |
| 194 | +</p> |
| 195 | +<p> |
| 196 | +sql2csv dbcfg.json ‘SELECT * FROM mytable LIMIT 10’<br /> |
| 197 | +>ten-rows.csv |
| 198 | +</p> |
| 199 | +<p> |
| 200 | +sql2csv 1.1.5 |
| 201 | +</p> |
| 202 | +</section> |
| 203 | + |
| 204 | +<footer> |
| 205 | +<span><h1><A href="http://caltech.edu">Caltech</a></h1></span> |
| 206 | +<span>© 2021 <a href="https://www.library.caltech.edu/copyright">Caltech library</a></span> |
| 207 | +<address>1200 E California Blvd, Mail Code 1-32, Pasadena, CA 91125-3200</address> |
| 208 | +<span>Phone: <a href="tel:+1-626-395-3405">(626)395-3405</a></span> |
| 209 | +<span><a href=" mailto:[email protected]" >Email Us </a></span> |
| 210 | +<a class="cl-hide" href="sitemap.xml">Site Map</a> |
| 211 | +</footer> |
| 212 | +</body> |
| 213 | +</html> |
0 commit comments