Browse Source

SQLite

pull/57/head
Jure Šorn 4 years ago
parent
commit
39c91beedd
2 changed files with 33 additions and 35 deletions
  1. 35
      README.md
  2. 33
      index.html

35
README.md

@ -1838,49 +1838,48 @@ SQLite
**Opens a connection to the database file. Creates a new file if path doesn't exist.**
```python
import sqlite3
db = sqlite3.connect('<path>') # Also ':memory:'.
...
db.close()
<con> = sqlite3.connect('<path>') # Also ':memory:'.
<con>.close()
```
### Read
**Returned values can be of type str, int, float, bytes or None.**
```python
<cursor> = db.execute('<query>') # Can raise a subclass of sqlite3.Error.
<cursor> = <con>.execute('<query>') # Can raise a subclass of sqlite3.Error.
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
<list> = <cursor>.fetchall() # Returns remaining rows. Also list(<cursor>).
```
### Write
```python
db.execute('<query>')
db.commit()
<con>.execute('<query>')
<con>.commit()
```
#### Or:
```python
with db:
db.execute('<query>')
with <con>:
<con>.execute('<query>')
```
### Placeholders
* **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](#encode).**
```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.
<con>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
<con>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
<con>.executemany('<query>', <coll_of_above>) # Runs execute() many times.
```
### Example
**In this example values are not actually saved because `'db.commit()'` is omitted!**
**In this example values are not actually saved because `'con.commit()'` is omitted!**
```python
>>> db = sqlite3.connect('test.db')
>>> db.execute('create table person (person_id integer primary key, name, height)')
>>> db.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
>>> con = sqlite3.connect('test.db')
>>> con.execute('create table person (person_id integer primary key, name, height)')
>>> con.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
1
>>> db.execute('select * from person').fetchall()
>>> con.execute('select * from person').fetchall()
[(1, 'Jean-Luc', 187)]
```
@ -1889,8 +1888,8 @@ db.executemany('<query>', <coll_of_above>) # Runs execute() many times.
```python
# $ pip3 install mysql-connector
from mysql import connector
db = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
<cursor> = db.cursor() # Only cursor has execute method.
<con> = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
<cursor> = <con>.cursor() # Only cursor has execute method.
<cursor>.execute('<query>') # Can raise a subclass of connector.Error.
<cursor>.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
<cursor>.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.

33
index.html

@ -1642,50 +1642,49 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</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 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
db = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
...
db.close()
&lt;con&gt; = sqlite3.connect(<span class="hljs-string">'&lt;path&gt;'</span>) <span class="hljs-comment"># Also ':memory:'.</span>
&lt;con&gt;.close()
</code></pre></div></div>
<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>
<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>
&lt;tuple&gt; = &lt;cursor&gt;.fetchone() <span class="hljs-comment"># Returns next row. Also next(&lt;cursor&gt;).</span>
&lt;list&gt; = &lt;cursor&gt;.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(&lt;cursor&gt;).</span>
</code></pre></div>
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
db.commit()
<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>)
&lt;con&gt;.commit()
</code></pre></div>
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> db:
db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;con&gt;:
&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
</code></pre></div>
<div><h3 id="placeholders">Placeholders</h3><ul>
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
</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>
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>
</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>
&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>
&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>
</code></pre></div>
<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>)
<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>)
<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
<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>)
<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>)
<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
<span class="hljs-number">1</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>db.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
<span class="hljs-meta">&gt;&gt;&gt; </span>con.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
[(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
</code></pre></div>
<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
db = connector.connect(host=&lt;str&gt;, …) <span class="hljs-comment"># `user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;`.</span>
&lt;cursor&gt; = db.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
&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>
&lt;cursor&gt; = &lt;con&gt;.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</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>

Loading…
Cancel
Save