|
54 | 54 |
|
55 | 55 | <body>
|
56 | 56 | <header>
|
57 |
| - <aside>September 16, 2022</aside> |
| 57 | + <aside>September 19, 2022</aside> |
58 | 58 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
59 | 59 | </header>
|
60 | 60 |
|
|
1557 | 1557 | writer.writerows(rows)
|
1558 | 1558 | </code></pre></div>
|
1559 | 1559 |
|
1560 |
| -<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>A server-less database engine that stores each database into a separate file.</strong></p><div><h3 id="connect">Connect</h3><p><strong>Opens a connection to the database file. Creates a new file if path doesn't exist.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
1561 |
| -<conn> = sqlite3.connect(<path>) <span class="hljs-comment"># Also ':memory:'.</span> |
| 1560 | +<div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>A server-less database engine that stores each database into a separate file.</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3 |
| 1561 | +<conn> = sqlite3.connect(<path>) <span class="hljs-comment"># Opens existing or new file. Also ':memory:'.</span> |
1562 | 1562 | <conn>.close() <span class="hljs-comment"># Closes the connection.</span>
|
1563 |
| -</code></pre></div></div> |
1564 |
| - |
1565 |
| - |
| 1563 | +</code></pre></div> |
1566 | 1564 |
|
1567 | 1565 |
|
1568 |
| -<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> = <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span> |
| 1566 | +<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs"><cursor> = <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span> |
1569 | 1567 | <tuple> = <cursor>.fetchone() <span class="hljs-comment"># Returns next row. Also next(<cursor>).</span>
|
1570 | 1568 | <list> = <cursor>.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(<cursor>).</span>
|
1571 | 1569 | </code></pre></div>
|
1572 | 1570 |
|
1573 |
| - |
1574 | 1571 | <div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
|
1575 | 1572 | <conn>.commit() <span class="hljs-comment"># Saves all changes since the last commit.</span>
|
1576 | 1573 | <conn>.rollback() <span class="hljs-comment"># Discards all changes since the last commit.</span>
|
|
1580 | 1577 | <conn>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># depending on whether any exception occurred.</span>
|
1581 | 1578 | </code></pre></div>
|
1582 | 1579 |
|
1583 |
| -<div><h3 id="placeholders">Placeholders</h3><ul> |
1584 |
| -<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.</strong></li> |
1585 |
| -<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li> |
1586 |
| -</ul><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
| 1580 | +<div><h3 id="placeholders">Placeholders</h3><pre><code class="python language-python hljs"><conn>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
1587 | 1581 | <conn>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span>
|
1588 | 1582 | <conn>.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() multiple times.</span>
|
1589 | 1583 | </code></pre></div>
|
1590 | 1584 |
|
1591 |
| - |
| 1585 | +<ul> |
| 1586 | +<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.</strong></li> |
| 1587 | +<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li> |
| 1588 | +</ul> |
1592 | 1589 | <div><h3 id="example">Example</h3><p><strong>Values are not actually saved in this example because <code class="python hljs"><span class="hljs-string">'conn.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>conn = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
|
1593 | 1590 | <span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'CREATE TABLE person (person_id INTEGER PRIMARY KEY, name, height)'</span>)
|
1594 | 1591 | <span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'INSERT INTO person VALUES (NULL, ?, ?)'</span>, (<span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)).lastrowid
|
@@ -2906,7 +2903,7 @@ <h3 id="format-2">Format</h3><div><h4 id="forstandardtypesizesandmanualalignment
|
2906 | 2903 |
|
2907 | 2904 |
|
2908 | 2905 | <footer>
|
2909 |
| - <aside>September 16, 2022</aside> |
| 2906 | + <aside>September 19, 2022</aside> |
2910 | 2907 | <a href="https://gto76.github.io" rel="author">Jure Šorn</a>
|
2911 | 2908 | </footer>
|
2912 | 2909 |
|
|
0 commit comments