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

Skip to content

Commit ff37d93

Browse files
committed
SQLite
1 parent 092a87a commit ff37d93

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,48 +1841,48 @@ SQLite
18411841
**Opens a connection to the database file. Creates a new file if path doesn't exist.**
18421842
```python
18431843
import sqlite3
1844-
<con> = sqlite3.connect(<path>) # Also ':memory:'.
1845-
<con>.close() # Closes the connection.
1844+
<conn> = sqlite3.connect(<path>) # Also ':memory:'.
1845+
<conn>.close() # Closes the connection.
18461846
```
18471847

18481848
### Read
18491849
**Returned values can be of type str, int, float, bytes or None.**
18501850
```python
1851-
<cursor> = <con>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1851+
<cursor> = <conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
18521852
<tuple> = <cursor>.fetchone() # Returns next row. Also next(<cursor>).
18531853
<list> = <cursor>.fetchall() # Returns remaining rows. Also list(<cursor>).
18541854
```
18551855

18561856
### Write
18571857
```python
1858-
<con>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1859-
<con>.commit() # Commits all transactions since last commit.
1858+
<conn>.execute('<query>') # Can raise a subclass of sqlite3.Error.
1859+
<conn>.commit() # Commits all transactions since last commit.
18601860
```
18611861

18621862
#### Or:
18631863
```python
1864-
with <con>:
1865-
<con>.execute('<query>')
1864+
with <conn>:
1865+
<conn>.execute('<query>')
18661866
```
18671867

18681868
### Placeholders
18691869
* **Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.**
18701870
* **Bools will be stored and returned as ints and dates as [ISO formatted strings](#encode).**
18711871
```python
1872-
<con>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1873-
<con>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1874-
<con>.executemany('<query>', <coll_of_above>) # Runs execute() multiple times.
1872+
<conn>.execute('<query>', <list/tuple>) # Replaces '?'s in query with values.
1873+
<conn>.execute('<query>', <dict/namedtuple>) # Replaces ':<key>'s with values.
1874+
<conn>.executemany('<query>', <coll_of_above>) # Runs execute() multiple times.
18751875
```
18761876

18771877
### Example
1878-
**In this example values are not actually saved because `'con.commit()'` is omitted!**
1878+
**In this example values are not actually saved because `'conn.commit()'` is omitted!**
18791879

18801880
```python
1881-
>>> con = sqlite3.connect('test.db')
1882-
>>> con.execute('create table person (person_id integer primary key, name, height)')
1883-
>>> con.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
1881+
>>> conn = sqlite3.connect('test.db')
1882+
>>> conn.execute('create table person (person_id integer primary key, name, height)')
1883+
>>> conn.execute('insert into person values (null, ?, ?)', ('Jean-Luc', 187)).lastrowid
18841884
1
1885-
>>> con.execute('select * from person').fetchall()
1885+
>>> conn.execute('select * from person').fetchall()
18861886
[(1, 'Jean-Luc', 187)]
18871887
```
18881888

@@ -1891,8 +1891,8 @@ with <con>:
18911891
```python
18921892
# $ pip3 install mysql-connector
18931893
from mysql import connector
1894-
<con> = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
1895-
<cursor> = <con>.cursor() # Only cursor has execute method.
1894+
<conn> = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
1895+
<cursor> = <conn>.cursor() # Only cursor has execute method.
18961896
<cursor>.execute('<query>') # Can raise a subclass of connector.Error.
18971897
<cursor>.execute('<query>', <list/tuple>) # Replaces '%s's in query with values.
18981898
<cursor>.execute('<query>', <dict/namedtuple>) # Replaces '%(<key>)s's with values.

index.html

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,49 +1659,49 @@
16591659
</code></pre></div>
16601660

16611661
<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
1662-
&lt;con&gt; = sqlite3.connect(&lt;path&gt;) <span class="hljs-comment"># Also ':memory:'.</span>
1663-
&lt;con&gt;.close() <span class="hljs-comment"># Closes the connection.</span>
1662+
&lt;conn&gt; = sqlite3.connect(&lt;path&gt;) <span class="hljs-comment"># Also ':memory:'.</span>
1663+
&lt;conn&gt;.close() <span class="hljs-comment"># Closes the connection.</span>
16641664
</code></pre></div></div>
16651665

16661666

16671667

16681668

1669-
<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>
1669+
<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;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
16701670
&lt;tuple&gt; = &lt;cursor&gt;.fetchone() <span class="hljs-comment"># Returns next row. Also next(&lt;cursor&gt;).</span>
16711671
&lt;list&gt; = &lt;cursor&gt;.fetchall() <span class="hljs-comment"># Returns remaining rows. Also list(&lt;cursor&gt;).</span>
16721672
</code></pre></div>
16731673

16741674

1675-
<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>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
1676-
&lt;con&gt;.commit() <span class="hljs-comment"># Commits all transactions since last commit.</span>
1675+
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
1676+
&lt;conn&gt;.commit() <span class="hljs-comment"># Commits all transactions since last commit.</span>
16771677
</code></pre></div>
16781678

1679-
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;con&gt;:
1680-
&lt;con&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1679+
<div><h4 id="or">Or:</h4><pre><code class="python language-python hljs"><span class="hljs-keyword">with</span> &lt;conn&gt;:
1680+
&lt;conn&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
16811681
</code></pre></div>
16821682

16831683
<div><h3 id="placeholders">Placeholders</h3><ul>
16841684
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetme.</strong></li>
16851685
<li><strong>Bools will be stored and returned as ints and dates as <a href="#encode">ISO formatted strings</a>.</strong></li>
1686-
</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>
1687-
&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>
1688-
&lt;con&gt;.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() multiple times.</span>
1686+
</ul><pre><code class="python language-python hljs">&lt;conn&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>
1687+
&lt;conn&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>
1688+
&lt;conn&gt;.executemany(<span class="hljs-string">'&lt;query&gt;'</span>, &lt;coll_of_above&gt;) <span class="hljs-comment"># Runs execute() multiple times.</span>
16891689
</code></pre></div>
16901690

16911691

1692-
<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>)
1693-
<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>)
1694-
<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
1692+
<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">'conn.commit()'</span></code> is omitted!</strong></p><pre><code class="python language-python hljs"><span class="hljs-meta">&gt;&gt;&gt; </span>conn = sqlite3.connect(<span class="hljs-string">'test.db'</span>)
1693+
<span class="hljs-meta">&gt;&gt;&gt; </span>conn.execute(<span class="hljs-string">'create table person (person_id integer primary key, name, height)'</span>)
1694+
<span class="hljs-meta">&gt;&gt;&gt; </span>conn.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
16951695
<span class="hljs-number">1</span>
1696-
<span class="hljs-meta">&gt;&gt;&gt; </span>con.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
1696+
<span class="hljs-meta">&gt;&gt;&gt; </span>conn.execute(<span class="hljs-string">'select * from person'</span>).fetchall()
16971697
[(<span class="hljs-number">1</span>, <span class="hljs-string">'Jean-Luc'</span>, <span class="hljs-number">187</span>)]
16981698
</code></pre></div>
16991699

17001700

17011701
<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>
17021702
<span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
1703-
&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>
1704-
&lt;cursor&gt; = &lt;con&gt;.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
1703+
&lt;conn&gt; = connector.connect(host=&lt;str&gt;, …) <span class="hljs-comment"># `user=&lt;str&gt;, password=&lt;str&gt;, database=&lt;str&gt;`.</span>
1704+
&lt;cursor&gt; = &lt;conn&gt;.cursor() <span class="hljs-comment"># Only cursor has execute method.</span>
17051705
&lt;cursor&gt;.execute(<span class="hljs-string">'&lt;query&gt;'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</span>
17061706
&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>
17071707
&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)