Skip to content

Commit 3ecb22f

Browse files
committed
SQLite
1 parent 874ae3e commit 3ecb22f

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

README.md

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -929,21 +929,22 @@ class <name>:
929929
* **Return value of repr() should be unambiguous and of str() readable.**
930930
* **If only repr() is defined, it will also be used for str().**
931931

932-
#### Str() is used by:
932+
#### Str() use cases:
933933
```python
934934
print(<el>)
935-
f'{<el>}'
935+
print(f'{<el>}')
936936
raise Exception(<el>)
937937
logging.debug(<el>)
938938
csv.writer(<file>).writerow([<el>])
939939
```
940940

941-
#### Repr() is used by:
941+
#### Repr() use cases:
942942
```python
943943
print([<el>])
944-
f'{<el>!r}'
944+
print(f'{<el>!r}')
945945
>>> <el>
946946
loguru.logger.exception()
947+
Z = dataclasses.make_dataclass('Z', ['a']); print(Z(<el>))
947948
```
948949

949950
### Constructor Overloading
@@ -1502,12 +1503,14 @@ def write_to_pickle_file(filename, an_object):
15021503

15031504
SQLite
15041505
------
1506+
**Server-less database engine that stores each database into separate file.**
15051507
```python
15061508
import sqlite3
15071509
db = sqlite3.connect('<path>') # Also ':memory:'.
15081510
...
15091511
db.close()
15101512
```
1513+
* **New database will be created if path doesn't exist.**
15111514

15121515
### Read
15131516
```python
@@ -1516,7 +1519,7 @@ if cursor:
15161519
<tuple> = cursor.fetchone() # First row.
15171520
<list> = cursor.fetchall() # Remaining rows.
15181521
```
1519-
* **Returned values can be of type str, int, float or bytes.**
1522+
* **Returned values can be of type str, int, float, bytes or None.**
15201523

15211524
### Write
15221525
```python
@@ -1526,10 +1529,20 @@ db.commit()
15261529

15271530
### Placeholders
15281531
```python
1529-
db.execute('<query>', <list/tuple>) # Replaces '?' in query with value.
1530-
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>' with value.
1532+
db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1533+
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1534+
db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
1535+
```
1536+
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
1537+
1538+
### MySQL
1539+
```python
1540+
# $ pip3 install mysql-connector
1541+
from mysql import connector
1542+
db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
1543+
cursor = db.cursor()
1544+
cursor.execute('<query>')
15311545
```
1532-
* **Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.**
15331546

15341547

15351548
Bytes

index.html

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -883,18 +883,19 @@ <h2 id="class"><a href="#class" name="class">#</a>Class</h2>
883883
<li><strong>Return value of repr() should be unambiguous and of str() readable.</strong></li>
884884
<li><strong>If only repr() is defined, it will also be used for str().</strong></li>
885885
</ul>
886-
<h4 id="strisusedby">Str() is used by:</h4>
886+
<h4 id="strusecases">Str() use cases:</h4>
887887
<pre><code class="python language-python hljs">print(&lt;el&gt;)
888-
<span class="hljs-string">f'<span class="hljs-subst">{&lt;el&gt;}</span>'</span>
888+
print(<span class="hljs-string">f'<span class="hljs-subst">{&lt;el&gt;}</span>'</span>)
889889
<span class="hljs-keyword">raise</span> Exception(&lt;el&gt;)
890890
logging.debug(&lt;el&gt;)
891891
csv.writer(&lt;file&gt;).writerow([&lt;el&gt;])
892892
</code></pre>
893-
<h4 id="reprisusedby">Repr() is used by:</h4>
893+
<h4 id="reprusecases">Repr() use cases:</h4>
894894
<pre><code class="python language-python hljs">print([&lt;el&gt;])
895-
<span class="hljs-string">f'<span class="hljs-subst">{&lt;el&gt;!r}</span>'</span>
895+
print(<span class="hljs-string">f'<span class="hljs-subst">{&lt;el&gt;!r}</span>'</span>)
896896
<span class="hljs-meta">&gt;&gt;&gt; </span>&lt;el&gt;
897897
loguru.logger.exception()
898+
Z = dataclasses.make_dataclass(<span class="hljs-string">'Z'</span>, [<span class="hljs-string">'a'</span>]); print(Z(&lt;el&gt;))
898899
</code></pre>
899900
<h3 id="constructoroverloading">Constructor Overloading</h3>
900901
<pre><code class="python language-python hljs"><span class="hljs-class"><span class="hljs-keyword">class</span> &lt;<span class="hljs-title">name</span>&gt;:</span>
@@ -1323,31 +1324,43 @@ <h3 id="writeobjecttofile">Write Object to File</h3>
13231324
pickle.dump(an_object, file)
13241325
</code></pre>
13251326
<h2 id="sqlite"><a href="#sqlite" name="sqlite">#</a>SQLite</h2>
1327+
<p><strong>Server-less database engine that stores each database into separate file.</strong></p>
13261328
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> sqlite3
13271329
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
13281330
...
13291331
db.close()
13301332
</code></pre>
1333+
<ul>
1334+
<li><strong>New database will be created if path doesn't exist.</strong></li>
1335+
</ul>
13311336
<h3 id="read">Read</h3>
13321337
<pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
13331338
<span class="hljs-keyword">if</span> cursor:
13341339
&lt;tuple&gt; = cursor.fetchone() <span class="hljs-comment"># First row.</span>
13351340
&lt;list&gt; = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span>
13361341
</code></pre>
13371342
<ul>
1338-
<li><strong>Returned values can be of type str, int, float or bytes.</strong></li>
1343+
<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li>
13391344
</ul>
13401345
<h3 id="write">Write</h3>
13411346
<pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
13421347
db.commit()
13431348
</code></pre>
13441349
<h3 id="placeholders">Placeholders</h3>
1345-
<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 '?' in query with value.</span>
1346-
db.execute(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;dict/namedtuple&gt;) <span class="hljs-comment"># Replaces ':&lt;key&gt;' with value.</span>
1350+
<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>
1351+
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>
1352+
db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
13471353
</code></pre>
13481354
<ul>
1349-
<li><strong>Passed values can be of type str, int, float, bytes, bool, datetime.date and datetime.datetme.</strong></li>
1355+
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
13501356
</ul>
1357+
<h3 id="mysql">MySQL</h3>
1358+
<pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install mysql-connector</span>
1359+
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
1360+
db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;)
1361+
cursor = db.cursor()
1362+
cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1363+
</code></pre>
13511364
<h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2>
13521365
<p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called 'bytearray'.</strong></p>
13531366
<pre><code class="python language-python hljs">&lt;bytes&gt; = <span class="hljs-string">b'&lt;str&gt;'</span> <span class="hljs-comment"># Only accepts ASCII characters and \x00 - \xff.</span>

0 commit comments

Comments
 (0)