Browse Source

SQLite

pull/42/head
Jure Šorn 5 years ago
parent
commit
73c904a773
2 changed files with 22 additions and 24 deletions
  1. 23
      README.md
  2. 23
      index.html

23
README.md

@ -1780,7 +1780,7 @@ SQLite
**Server-less database engine that stores each database into separate file.** **Server-less database engine that stores each database into separate file.**
```python ```python
import sqlite3 import sqlite3
db = sqlite3.connect('<path>') # Also ':memory:'.
db = sqlite3.connect('<path>') # Also ':memory:'.
... ...
db.close() db.close()
``` ```
@ -1788,10 +1788,9 @@ db.close()
### Read ### Read
```python ```python
cursor = db.execute('<query>')
if cursor:
<tuple> = cursor.fetchone() # First row. Also next(cursor).
<list> = cursor.fetchall() # Remaining rows.
<cursor> = db.execute('<query>') # Raises sqlite3.OperationalError.
<tuple> = <cursor>.fetchone() # First row. Also next(<cursor>).
<list> = <cursor>.fetchall() # Remaining rows.
``` ```
* **Returned values can be of type str, int, float, bytes or None.** * **Returned values can be of type str, int, float, bytes or None.**
@ -1809,9 +1808,9 @@ with db:
### Placeholders ### Placeholders
```python ```python
db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
db.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
db.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
``` ```
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.** * **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
* **Bools will be stored and returned as ints and dates as ISO formatted strings.** * **Bools will be stored and returned as ints and dates as ISO formatted strings.**
@ -1832,10 +1831,10 @@ db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
# $ pip3 install mysql-connector # $ pip3 install mysql-connector
from mysql import connector from mysql import connector
db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>) db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)
cursor = db.cursor()
cursor.execute('<query>') # Only cursor has execute method.
cursor.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
cursor.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.
<cursor> = db.cursor()
<cursor>.execute('<query>') # Only cursor has execute method.
<cursor>.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
<cursor>.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.
``` ```

23
index.html

@ -1587,7 +1587,7 @@ shutil.rmtree(&lt;path&gt;) <span class="hljs-comment"># Deletes th
</code></pre></div> </code></pre></div>
<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 <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
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
... ...
db.close() db.close()
</code></pre></div> </code></pre></div>
@ -1596,10 +1596,9 @@ db.close()
<ul> <ul>
<li><strong>New database will be created if path doesn't exist.</strong></li> <li><strong>New database will be created if path doesn't exist.</strong></li>
</ul> </ul>
<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
<span class="hljs-keyword">if</span> cursor:
&lt;tuple&gt; = cursor.fetchone() <span class="hljs-comment"># First row. Also next(cursor).</span>
&lt;list&gt; = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span>
<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"># Raises sqlite3.OperationalError.</span>
&lt;tuple&gt; = &lt;cursor&gt;.fetchone() <span class="hljs-comment"># First row. Also next(&lt;cursor&gt;).</span>
&lt;list&gt; = &lt;cursor&gt;.fetchall() <span class="hljs-comment"># Remaining rows.</span>
</code></pre></div> </code></pre></div>
<ul> <ul>
@ -1613,9 +1612,9 @@ db.commit()
db.execute(<span class="hljs-string">'&lt;query&gt;'</span>) db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
</code></pre></div> </code></pre></div>
<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>
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>
db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
<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>
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>
db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() many times.</span>
</code></pre></div> </code></pre></div>
<ul> <ul>
@ -1635,10 +1634,10 @@ db.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_abo
<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> <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>
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector <span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;) db = connector.connect(host=&lt;str&gt;, user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;)
cursor = db.cursor()
cursor.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Only cursor has execute method.</span>
cursor.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>
cursor.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>
&lt;cursor&gt; = db.cursor()
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Only cursor has execute method.</span>
&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>
&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>
</code></pre></div> </code></pre></div>

Loading…
Cancel
Save