Skip to content

Commit 39c91be

Browse files
committed
SQLite
1 parent 716a966 commit 39c91be

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

README.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,49 +1838,48 @@ SQLite
18381838
**Opens a connection to the database file. Creates a new file if path doesn't exist.**
18391839
```python
18401840
import sqlite3
1841-
db = sqlite3.connect('<path>') # Also ':memory:'.
1842-
...
1843-
db.close()
1841+
<con> = sqlite3.connect('<path>') # Also ':memory:'.
1842+
<con>.close()
18441843
```
18451844

18461845
### Read
18471846
**Returned values can be of type str, int, float, bytes or None.**
18481847
```python
1849-
<cursor> = db.execute('<query>') # Can raise a subclass of sqlite3.Error.
1848+
<cursor> = <con>.execute('<query>') # Can raise a subclass of sqlite3.Error.
18501849
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
18511850
<list> = <cursor>.fetchall() # Returns remaining rows. Also list(<cursor>).
18521851
```
18531852

18541853
### Write
18551854
```python
1856-
db.execute('<query>')
1857-
db.commit()
1855+
<con>.execute('<query>')
1856+
<con>.commit()
18581857
```
18591858

18601859
#### Or:
18611860
```python
1862-
with db:
1863-
db.execute('<query>')
1861+
with <con>:
1862+
<con>.execute('<query>')
18641863
```
18651864

18661865
### Placeholders
18671866
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
18681867
* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).**
18691868
```python
1870-
db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1871-
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1872-
db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
1869+
<con>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1870+
<con>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1871+
<con>.executemany('<query>', <coll_of_above>) # Runs execute() many times.
18731872
```
18741873

18751874
### Example
1876-
**In this example values are not actually saved because `'db.commit()'` is omitted!**
1875+
**In this example values are not actually saved because `'con.commit()'` is omitted!**
18771876

18781877
```python
1879-
>>> db = sqlite3.connect('test.db')
1880-
>>> db.execute('create table person (person_id integer primary key, name, height)')
1881-
>>> db.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
1878+
>>> con = sqlite3.connect('test.db')
1879+
>>> con.execute('create table person (person_id integer primary key, name, height)')
1880+
>>> con.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
18821881
1
1883-
>>> db.execute('select * from person').fetchall()
1882+
>>> con.execute('select * from person').fetchall()
18841883
[(1, 'Jean-Luc', 187)]
18851884
```
18861885

@@ -1889,8 +1888,8 @@ db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
18891888
```python
18901889
# $ pip3 install mysql-connector
18911890
from mysql import connector
1892-
db = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
1893-
<cursor> = db.cursor() # Only cursor has execute method.
1891+
<con> = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
1892+
<cursor> = <con>.cursor() # Only cursor has execute method.
18941893
<cursor>.execute('<query>') # Can raise a subclass of connector.Error.
18951894
<cursor>.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
18961895
<cursor>.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.

index.html

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1642,50 +1642,49 @@
16421642
</code></pre></div>
16431643

16441644
<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">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
1646-
...
1647-
db.close()
1645+
&lt;con&gt; = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
1646+
&lt;con&gt;.close()
16481647
</code></pre></div></div>
16491648

16501649

16511650

16521651

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">&lt;cursor&gt; = db.execute(<span class="hljs-string">'&lt;query&gt;'</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">&lt;cursor&gt; = &lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
16541653
&lt;tuple&gt; = &lt;cursor&gt;.fetchone() <span class="hljs-comment"># Returns next row. Also next(&lt;cursor&gt;).</span>
16551654
&lt;list&gt; = &lt;cursor&gt;.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(&lt;cursor&gt;).</span>
16561655
</code></pre></div>
16571656

16581657

1659-
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1660-
db.commit()
1658+
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1659+
&lt;con&gt;.commit()
16611660
</code></pre></div>
16621661

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">'&lt;query&gt;'</span>)
1662+
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;con&gt;:
1663+
&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
16651664
</code></pre></div>
16661665

16671666
<div><h3 id="placeholders">Placeholders</h3><ul>
16681667
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
16691668
<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">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
1671-
db.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces ':&lt;key&gt;'s with values.</span>
1672-
db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
1669+
</ul><pre><code class="python language-python hljs">&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
1670+
&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces ':&lt;key&gt;'s with values.</span>
1671+
&lt;con&gt;.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
16731672
</code></pre></div>
16741673

16751674

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">&gt;&gt;&gt; </span>db = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
1677-
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>)
1678-
<span class="hljs-meta">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span>con = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
1676+
<span class="hljs-meta">&gt;&gt;&gt; </span>con.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>)
1677+
<span class="hljs-meta">&gt;&gt;&gt; </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
16791678
<span class="hljs-number">1</span>
1680-
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
1679+
<span class="hljs-meta">&gt;&gt;&gt; </span>con.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
16811680
[(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
16821681
</code></pre></div>
16831682

16841683

16851684
<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>
16861685
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
1687-
db = connector.connect(host=&lt;str&gt;, …) <span class="hljs-comment"># `user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;`.</span>
1688-
&lt;cursor&gt; = db.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
1686+
&lt;con&gt; = connector.connect(host=&lt;str&gt;, …) <span class="hljs-comment"># `user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;`.</span>
1687+
&lt;cursor&gt; = &lt;con&gt;.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
16891688
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</span>
16901689
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '%s's in query with values.</span>
16911690
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces '%(&lt;key&gt;)s's with values.</span>

0 commit comments

Comments
 (0)