|
1351 | 1351 | <ul>
|
1352 | 1352 | <li><strong>Levels deeper than 'depth' get replaced by '…'.</strong></li>
|
1353 | 1353 | </ul>
|
1354 |
| -<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><ul> |
1355 |
| -<li><strong>Reads a line from user input or pipe if present.</strong></li> |
1356 |
| -<li><strong>Trailing newline gets stripped.</strong></li> |
1357 |
| -<li><strong>Prompt string is printed to the standard output before reading input.</strong></li> |
1358 |
| -<li><strong>Raises EOFError when user hits EOF or input stream gets exhausted.</strong></li> |
1359 |
| -</ul><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>) |
| 1354 | +<div><h2 id="input"><a href="#input" name="input">#</a>Input</h2><p><strong>Reads a line from user input or pipe if present.</strong></p><pre><code class="python language-python hljs"><str> = input(prompt=<span class="hljs-keyword">None</span>) |
1360 | 1355 | </code></pre></div>
|
1361 | 1356 |
|
1362 | 1357 |
|
| 1358 | +<ul> |
| 1359 | +<li><strong>Trailing newline gets stripped.</strong></li> |
| 1360 | +<li><strong>Prompt string is printed to the standard output before reading input.</strong></li> |
| 1361 | +<li><strong>Raises EOFError when user hits EOF or input stream gets exhausted.</strong></li> |
| 1362 | +</ul> |
1363 | 1363 | <div><h2 id="commandlinearguments"><a href="#commandlinearguments" name="commandlinearguments">#</a>Command Line Arguments</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sys
|
1364 | 1364 | script_name = sys.argv[<span class="hljs-number">0</span>]
|
1365 | 1365 | arguments = sys.argv[<span class="hljs-number">1</span>:]
|
|
1603 | 1603 | writer.writerows(rows)
|
1604 | 1604 | </code></pre></div>
|
1605 | 1605 |
|
1606 |
| -<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into separate file.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
| 1606 | +<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into separate file.</strong></p><div><h3 id="connect">Connect</h3><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
1607 | 1607 | db = sqlite3.connect(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
|
1608 | 1608 | ...
|
1609 | 1609 | db.close()
|
1610 |
| -</code></pre></div> |
| 1610 | +</code></pre></div></div> |
| 1611 | + |
1611 | 1612 |
|
1612 | 1613 |
|
1613 | 1614 | <ul>
|
1614 | 1615 | <li><strong>New database will be created if path doesn't exist.</strong></li>
|
1615 | 1616 | </ul>
|
1616 |
| -<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs"><cursor> = db.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise sqlite3.OperationalError.</span> |
| 1617 | +<div><h3 id="read-1">Read</h3><p><strong>Returned values can be of type str, int, float, bytes or None.</strong></p><pre><code class="python language-python hljs"><cursor> = db.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise sqlite3.OperationalError.</span> |
1617 | 1618 | <tuple> = <cursor>.fetchone() <span class="hljs-comment"># Returns next row. Also next(<cursor>).</span>
|
1618 | 1619 | <list> = <cursor>.fetchall() <span class="hljs-comment"># Returns remaining rows.</span>
|
1619 | 1620 | </code></pre></div>
|
1620 | 1621 |
|
1621 |
| -<ul> |
1622 |
| -<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li> |
1623 |
| -</ul> |
| 1622 | + |
1624 | 1623 | <div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>)
|
1625 | 1624 | db.commit()
|
1626 | 1625 | </code></pre></div>
|
|
1629 | 1628 | db.execute(<span class="hljs-string">'<query>'</span>)
|
1630 | 1629 | </code></pre></div>
|
1631 | 1630 |
|
1632 |
| -<div><h3 id="placeholders">Placeholders</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
| 1631 | +<div><h3 id="placeholders">Placeholders</h3><ul> |
| 1632 | +<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li> |
| 1633 | +<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li> |
| 1634 | +</ul><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
1633 | 1635 | db.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span>
|
1634 | 1636 | db.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() many times.</span>
|
1635 | 1637 | </code></pre></div>
|
1636 | 1638 |
|
1637 |
| -<ul> |
1638 |
| -<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li> |
1639 |
| -<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li> |
1640 |
| -</ul> |
1641 |
| -<div><h3 id="example">Example</h3><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>db = sqlite3.connect(<span class="hljs-string">'test.db'</span>) |
| 1639 | + |
| 1640 | +<div><h3 id="example">Example</h3><p><strong>In this example values are not actually saved because <code class="python hljs"><span class="hljs-string">'db.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>db = sqlite3.connect(<span class="hljs-string">'test.db'</span>) |
1642 | 1641 | <span class="hljs-meta">>>> </span>db.execute(<span class="hljs-string">'create table t (a, b, c)'</span>)
|
1643 | 1642 | <span class="hljs-meta">>>> </span>db.execute(<span class="hljs-string">'insert into t values (1, 2, 3)'</span>)
|
1644 | 1643 | <span class="hljs-meta">>>> </span>db.execute(<span class="hljs-string">'select * from t'</span>).fetchall()
|
1645 | 1644 | [(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)]
|
1646 | 1645 | </code></pre></div>
|
1647 | 1646 |
|
1648 |
| -<ul> |
1649 |
| -<li><strong>In this example values are not actually saved because <code class="python hljs"><span class="hljs-string">'db.commit()'</span></code> was omitted.</strong> </li> |
1650 |
| -</ul> |
| 1647 | + |
1651 | 1648 | <div><h3 id="mysql">MySQL</h3><p><strong>Has a very similar interface, with differences listed below.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install mysql-connector</span>
|
1652 | 1649 | <span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
|
1653 | 1650 | db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
|
|
0 commit comments