Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd3cfe5

Browse files
committed
SQLite
1 parent a4747b5 commit bd3cfe5

File tree

2 files changed

+29
-31
lines changed

2 files changed

+29
-31
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,14 +1456,13 @@ pprint(<collection>, width=80, depth=None)
14561456

14571457
Input
14581458
-----
1459-
* **Reads a line from user input or pipe if present.**
1460-
* **Trailing newline gets stripped.**
1461-
* **Prompt string is printed to the standard output before reading input.**
1462-
* **Raises EOFError when user hits EOF or input stream gets exhausted.**
1463-
1459+
**Reads a line from user input or pipe if present.**
14641460
```python
14651461
<str> = input(prompt=None)
14661462
```
1463+
* **Trailing newline gets stripped.**
1464+
* **Prompt string is printed to the standard output before reading input.**
1465+
* **Raises EOFError when user hits EOF or input stream gets exhausted.**
14671466

14681467

14691468
Command Line Arguments
@@ -1795,6 +1794,8 @@ def write_to_csv_file(filename, rows):
17951794
SQLite
17961795
------
17971796
**Server-less database engine that stores each database into separate file.**
1797+
1798+
### Connect
17981799
```python
17991800
import sqlite3
18001801
db = sqlite3.connect('<path>') # Also ':memory:'.
@@ -1804,12 +1805,12 @@ db.close()
18041805
* **New database will be created if path doesn't exist.**
18051806

18061807
### Read
1808+
**Returned values can be of type str, int, float, bytes or None.**
18071809
```python
18081810
<cursor> = db.execute('<query>') # Can raise sqlite3.OperationalError.
18091811
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
18101812
<list> = <cursor>.fetchall() # Returns remaining rows.
18111813
```
1812-
* **Returned values can be of type str, int, float, bytes or None.**
18131814

18141815
### Write
18151816
```python
@@ -1824,23 +1825,23 @@ with db:
18241825
```
18251826

18261827
### Placeholders
1828+
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
1829+
* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).**
18271830
```python
18281831
db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
18291832
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
18301833
db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
18311834
```
1832-
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
1833-
* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).**
18341835

18351836
### Example
1837+
**In this example values are not actually saved because `'db.commit()'` is omitted!**
18361838
```python
18371839
>>> db = sqlite3.connect('test.db')
18381840
>>> db.execute('create table t (a, b, c)')
18391841
>>> db.execute('insert into t values (1, 2, 3)')
18401842
>>> db.execute('select * from t').fetchall()
18411843
[(1, 2, 3)]
1842-
```
1843-
* **In this example values are not actually saved because `'db.commit()'` was omitted.**
1844+
```
18441845

18451846
### MySQL
18461847
**Has a very similar interface, with differences listed below.**

index.html

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,15 +1351,15 @@
13511351
<ul>
13521352
<li><strong>Levels deeper than 'depth' get replaced by '…'.</strong></li>
13531353
</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">&lt;str&gt; = 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">&lt;str&gt; = input(prompt=<span class="hljs-keyword">None</span>)
13601355
</code></pre></div>
13611356

13621357

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>
13631363
<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
13641364
script_name = sys.argv[<span class="hljs-number">0</span>]
13651365
arguments = sys.argv[<span class="hljs-number">1</span>:]
@@ -1603,24 +1603,23 @@
16031603
writer.writerows(rows)
16041604
</code></pre></div>
16051605

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
16071607
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
16081608
...
16091609
db.close()
1610-
</code></pre></div>
1610+
</code></pre></div></div>
1611+
16111612

16121613

16131614
<ul>
16141615
<li><strong>New database will be created if path doesn't exist.</strong></li>
16151616
</ul>
1616-
<div><h3 id="read-1">Read</h3><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 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">&lt;cursor&gt; = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise sqlite3.OperationalError.</span>
16171618
&lt;tuple&gt; = &lt;cursor&gt;.fetchone() <span class="hljs-comment"># Returns next row. Also next(&lt;cursor&gt;).</span>
16181619
&lt;list&gt; = &lt;cursor&gt;.fetchall() <span class="hljs-comment"># Returns remaining rows.</span>
16191620
</code></pre></div>
16201621

1621-
<ul>
1622-
<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li>
1623-
</ul>
1622+
16241623
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
16251624
db.commit()
16261625
</code></pre></div>
@@ -1629,25 +1628,23 @@
16291628
db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
16301629
</code></pre></div>
16311630

1632-
<div><h3 id="placeholders">Placeholders</h3><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>
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">'&lt;query&gt;'</span>, &lt;list/tuple&gt;) <span class="hljs-comment"># Replaces '?'s in query with values.</span>
16331635
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>
16341636
db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
16351637
</code></pre></div>
16361638

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">&gt;&gt;&gt; </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">&gt;&gt;&gt; </span>db = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
16421641
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'create table t (a, b, c)'</span>)
16431642
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'insert into t values (1, 2, 3)'</span>)
16441643
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'select * from t'</span>).fetchall()
16451644
[(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>)]
16461645
</code></pre></div>
16471646

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+
16511648
<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>
16521649
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
16531650
db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;)

0 commit comments

Comments
 (0)