|
1642 | 1642 | </code></pre></div>
|
1643 | 1643 |
|
1644 | 1644 | <div><h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2><p><strong>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
|
1645 |
| -db = sqlite3.connect(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span> |
1646 |
| -... |
1647 |
| -db.close() |
| 1645 | +<con> = sqlite3.connect(<span class="hljs-string">'<path>'</span>) <span class="hljs-comment"># Also ':memory:'.</span> |
| 1646 | +<con>.close() |
1648 | 1647 | </code></pre></div></div>
|
1649 | 1648 |
|
1650 | 1649 |
|
1651 | 1650 |
|
1652 | 1651 |
|
1653 |
| -<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 a subclass of sqlite3.Error.</span> |
| 1652 | +<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> = <con>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span> |
1654 | 1653 | <tuple> = <cursor>.fetchone() <span class="hljs-comment"># Returns next row. Also next(<cursor>).</span>
|
1655 | 1654 | <list> = <cursor>.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(<cursor>).</span>
|
1656 | 1655 | </code></pre></div>
|
1657 | 1656 |
|
1658 | 1657 |
|
1659 |
| -<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>) |
1660 |
| -db.commit() |
| 1658 | +<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs"><con>.execute(<span class="hljs-string">'<query>'</span>) |
| 1659 | +<con>.commit() |
1661 | 1660 | </code></pre></div>
|
1662 | 1661 |
|
1663 |
| -<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> db: |
1664 |
| - db.execute(<span class="hljs-string">'<query>'</span>) |
| 1662 | +<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <con>: |
| 1663 | + <con>.execute(<span class="hljs-string">'<query>'</span>) |
1665 | 1664 | </code></pre></div>
|
1666 | 1665 |
|
1667 | 1666 | <div><h3 id="placeholders">Placeholders</h3><ul>
|
1668 | 1667 | <li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
|
1669 | 1668 | <li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
|
1670 |
| -</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> |
1671 |
| -db.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span> |
1672 |
| -db.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() many times.</span> |
| 1669 | +</ul><pre><code class="python language-python hljs"><con>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '?'s in query with values.</span> |
| 1670 | +<con>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span> |
| 1671 | +<con>.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() many times.</span> |
1673 | 1672 | </code></pre></div>
|
1674 | 1673 |
|
1675 | 1674 |
|
1676 |
| -<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>) |
1677 |
| -<span class="hljs-meta">>>> </span>db.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>) |
1678 |
| -<span class="hljs-meta">>>> </span>db.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 |
| 1675 | +<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">'con.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">>>> </span>con = sqlite3.connect(<span class="hljs-string">'test.db'</span>) |
| 1676 | +<span class="hljs-meta">>>> </span>con.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>) |
| 1677 | +<span class="hljs-meta">>>> </span>con.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 |
1679 | 1678 | <span class="hljs-number">1</span>
|
1680 |
| -<span class="hljs-meta">>>> </span>db.execute(<span class="hljs-string">'select * from person'</span>).fetchall() |
| 1679 | +<span class="hljs-meta">>>> </span>con.execute(<span class="hljs-string">'select * from person'</span>).fetchall() |
1681 | 1680 | [(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
|
1682 | 1681 | </code></pre></div>
|
1683 | 1682 |
|
1684 | 1683 |
|
1685 | 1684 | <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>
|
1686 | 1685 | <span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
|
1687 |
| -db = connector.connect(host=<str>, …) <span class="hljs-comment"># `user=<str>, password=<str>, database=<str>`.</span> |
1688 |
| -<cursor> = db.cursor() <span class="hljs-comment"># Only cursor has execute method.</span> |
| 1686 | +<con> = connector.connect(host=<str>, …) <span class="hljs-comment"># `user=<str>, password=<str>, database=<str>`.</span> |
| 1687 | +<cursor> = <con>.cursor() <span class="hljs-comment"># Only cursor has execute method.</span> |
1689 | 1688 | <cursor>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</span>
|
1690 | 1689 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '%s's in query with values.</span>
|
1691 | 1690 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces '%(<key>)s's with values.</span>
|
|
0 commit comments