|
1659 | 1659 | </code></pre></div>
|
1660 | 1660 |
|
1661 | 1661 | <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
|
1662 |
| -<con> = sqlite3.connect(<path>) <span class="hljs-comment"># Also ':memory:'.</span> |
1663 |
| -<con>.close() <span class="hljs-comment"># Closes the connection.</span> |
| 1662 | +<conn> = sqlite3.connect(<path>) <span class="hljs-comment"># Also ':memory:'.</span> |
| 1663 | +<conn>.close() <span class="hljs-comment"># Closes the connection.</span> |
1664 | 1664 | </code></pre></div></div>
|
1665 | 1665 |
|
1666 | 1666 |
|
1667 | 1667 |
|
1668 | 1668 |
|
1669 |
| -<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> |
| 1669 | +<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> |
1670 | 1670 | <tuple> = <cursor>.fetchone() <span class="hljs-comment"># Returns next row. Also next(<cursor>).</span>
|
1671 | 1671 | <list> = <cursor>.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(<cursor>).</span>
|
1672 | 1672 | </code></pre></div>
|
1673 | 1673 |
|
1674 | 1674 |
|
1675 |
| -<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs"><con>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span> |
1676 |
| -<con>.commit() <span class="hljs-comment"># Commits all transactions since last commit.</span> |
| 1675 | +<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> |
| 1676 | +<conn>.commit() <span class="hljs-comment"># Commits all transactions since last commit.</span> |
1677 | 1677 | </code></pre></div>
|
1678 | 1678 |
|
1679 |
| -<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <con>: |
1680 |
| - <con>.execute(<span class="hljs-string">'<query>'</span>) |
| 1679 | +<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> <conn>: |
| 1680 | + <conn>.execute(<span class="hljs-string">'<query>'</span>) |
1681 | 1681 | </code></pre></div>
|
1682 | 1682 |
|
1683 | 1683 | <div><h3 id="placeholders">Placeholders</h3><ul>
|
1684 | 1684 | <li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
|
1685 | 1685 | <li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
|
1686 |
| -</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> |
1687 |
| -<con>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span> |
1688 |
| -<con>.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() multiple times.</span> |
| 1686 | +</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> |
| 1687 | +<conn>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces ':<key>'s with values.</span> |
| 1688 | +<conn>.executemany(<span class="hljs-string">'<query>'</span>, <coll_of_above>) <span class="hljs-comment"># Runs execute() multiple times.</span> |
1689 | 1689 | </code></pre></div>
|
1690 | 1690 |
|
1691 | 1691 |
|
1692 |
| -<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>) |
1693 |
| -<span class="hljs-meta">>>> </span>con.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>) |
1694 |
| -<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 |
| 1692 | +<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">'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>) |
| 1693 | +<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>) |
| 1694 | +<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 |
1695 | 1695 | <span class="hljs-number">1</span>
|
1696 |
| -<span class="hljs-meta">>>> </span>con.execute(<span class="hljs-string">'select * from person'</span>).fetchall() |
| 1696 | +<span class="hljs-meta">>>> </span>conn.execute(<span class="hljs-string">'select * from person'</span>).fetchall() |
1697 | 1697 | [(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
|
1698 | 1698 | </code></pre></div>
|
1699 | 1699 |
|
1700 | 1700 |
|
1701 | 1701 | <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>
|
1702 | 1702 | <span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
|
1703 |
| -<con> = connector.connect(host=<str>, …) <span class="hljs-comment"># `user=<str>, password=<str>, database=<str>`.</span> |
1704 |
| -<cursor> = <con>.cursor() <span class="hljs-comment"># Only cursor has execute method.</span> |
| 1703 | +<conn> = connector.connect(host=<str>, …) <span class="hljs-comment"># `user=<str>, password=<str>, database=<str>`.</span> |
| 1704 | +<cursor> = <conn>.cursor() <span class="hljs-comment"># Only cursor has execute method.</span> |
1705 | 1705 | <cursor>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</span>
|
1706 | 1706 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '%s's in query with values.</span>
|
1707 | 1707 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces '%(<key>)s's with values.</span>
|
|
0 commit comments