Browse Source

Switched MySQL with SQLAlchemy

pull/135/head
Jure Šorn 2 years ago
parent
commit
bda49a998e
6 changed files with 1119 additions and 31 deletions
  1. 29
      README.md
  2. 27
      index.html
  3. 30
      parse.js
  4. 3
      pdf/index_for_pdf.html
  5. 3
      pdf/index_for_pdf_print.html
  6. 1058
      web/empty_script.py

29
README.md

@ -1914,16 +1914,25 @@ with <conn>: # Exits the block with commit()
[(1, 'Jean-Luc', 187)]
```
### MySQL
**Has a very similar interface, with differences listed below.**
```python
# $ pip3 install mysql-connector
from mysql import connector
<conn> = connector.connect(host=<str>, …) # `user=<str>, password=<str>, database=<str>`.
<cursor> = <conn>.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.
### SqlAlchemy
```python
# $ pip3 install sqlalchemy
from sqlalchemy import create_engine, text
<engine> = create_engine('<url>').connect() # Url: 'dialect://user:password@host/dbname'.
<conn> = <engine>.connect() # Creates a connection. Also <conn>.close().
<cursor> = <conn>.execute(text('<query>'), …) # Replaces ':<key>'s with keyword arguments.
with <conn>.begin(): ... # Exits the block with commit or rollback.
```
```text
+------------+--------------+-----------+-----------------------------------+
| Dialects | pip3 install | import | Dependencies |
+------------+--------------+-----------+-----------------------------------+
| mysql | mysqlclient | MySQLdb | www.pypi.org/project/mysqlclient |
| postgresql | psycopg2 | psycopg2 | www.psycopg.org/docs/install.html |
| mssql | pyodbc | pyodbc | apt install g++ unixodbc-dev |
| oracle | cx_oracle | cx_Oracle | Oracle Instant Client |
+------------+--------------+-----------+-----------------------------------+
```

27
index.html

@ -54,7 +54,7 @@
<body>
<header>
<aside>September 19, 2022</aside>
<aside>October 2, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</header>
@ -1595,16 +1595,23 @@ CompletedProcess(args=[<span class="hljs-string">'bc'</span>, <span class="hljs-
</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
&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>
&lt;cursor&gt; = &lt;conn&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>
<div><h3 id="sqlalchemy">SqlAlchemy</h3><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install sqlalchemy</span>
<span class="hljs-keyword">from</span> sqlalchemy <span class="hljs-keyword">import</span> create_engine, text
&lt;engine&gt; = create_engine(<span class="hljs-string">'&lt;url&gt;'</span>).connect() <span class="hljs-comment"># Url: 'dialect://user:password@host/dbname'.</span>
&lt;conn&gt; = &lt;engine&gt;.connect() <span class="hljs-comment"># Creates a connection. Also &lt;conn&gt;.close().</span>
&lt;cursor&gt; = &lt;conn&gt;.execute(text(<span class="hljs-string">'&lt;query&gt;'</span>), …) <span class="hljs-comment"># Replaces ':&lt;key&gt;'s with keyword arguments.</span>
<span class="hljs-keyword">with</span> &lt;conn&gt;.begin(): ... <span class="hljs-comment"># Exits the block with commit or rollback.</span>
</code></pre></div>
<pre><code class="text language-text">┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Dialects │ pip3 install │ import │ Dependencies ┃
┠────────────┼──────────────┼───────────┼───────────────────────────────────┨
┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃
┃ postgresql │ psycopg2 │ psycopg2 │ www.psycopg.org/docs/install.html ┃
┃ mssql │ pyodbc │ pyodbc │ apt install g++ unixodbc-dev ┃
┃ oracle │ cx_oracle │ cx_Oracle │ Oracle Instant Client ┃
┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
</code></pre>
<div><h2 id="bytes"><a href="#bytes" name="bytes">#</a>Bytes</h2><p><strong>Bytes object is an immutable sequence of single bytes. Mutable version is called bytearray.</strong></p><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>
&lt;int&gt; = &lt;bytes&gt;[&lt;index&gt;] <span class="hljs-comment"># Returns an int in range from 0 to 255.</span>
&lt;bytes&gt; = &lt;bytes&gt;[&lt;slice&gt;] <span class="hljs-comment"># Returns bytes even if it has only one element.</span>
@ -2903,7 +2910,7 @@ $ pyinstaller script.py --add-data '&lt;path&gt;:.' <span class="hljs-comment">
<footer>
<aside>September 19, 2022</aside>
<aside>October 2, 2022</aside>
<a href="https://gto76.github.io" rel="author">Jure Šorn</a>
</footer>

30
parse.js

@ -262,13 +262,13 @@ const DIAGRAM_1_A =
'+------------------+------------+------------+------------+\n';
const DIAGRAM_1_B =
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
'┃ │ Iterable │ Collection │ Sequence ┃\n' +
'┠──────────────────┼────────────┼────────────┼────────────┨\n' +
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ dict, set │ ✓ │ ✓ │ ┃\n' +
'┃ iter │ ✓ │ │ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
'┏━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┯━━━━━━━━━━━━┓\n' +
'┃ │ Iterable │ Collection │ Sequence ┃\n' +
'┠──────────────────┼────────────┼────────────┼────────────┨\n' +
'┃ list, range, str │ ✓ │ ✓ │ ✓ ┃\n' +
'┃ dict, set │ ✓ │ ✓ │ ┃\n' +
'┃ iter │ ✓ │ │ ┃\n' +
'┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┷━━━━━━━━━━━━┛\n';
const DIAGRAM_2_A =
'+--------------------+----------+----------+----------+----------+----------+\n' +
@ -414,6 +414,21 @@ const DIAGRAM_9_B =
"┃ escapechar │ None │ None │ None ┃\n" +
"┗━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━┛\n";
const DIAGRAM_95_A =
"+------------+--------------+-----------+-----------------------------------+\n" +
"| Dialects | pip3 install | import | Dependencies |\n" +
"+------------+--------------+-----------+-----------------------------------+\n";
const DIAGRAM_95_B =
"┏━━━━━━━━━━━━┯━━━━━━━━━━━━━━┯━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n" +
"┃ Dialects │ pip3 install │ import │ Dependencies ┃\n" +
"┠────────────┼──────────────┼───────────┼───────────────────────────────────┨\n" +
"┃ mysql │ mysqlclient │ MySQLdb │ www.pypi.org/project/mysqlclient ┃\n" +
"┃ postgresql │ psycopg2 │ psycopg2 │ www.psycopg.org/docs/install.html ┃\n" +
"┃ mssql │ pyodbc │ pyodbc │ apt install g++ unixodbc-dev ┃\n" +
"┃ oracle │ cx_oracle │ cx_Oracle │ Oracle Instant Client ┃\n" +
"┗━━━━━━━━━━━━┷━━━━━━━━━━━━━━┷━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
const DIAGRAM_10_A =
'+-------------+-------------+\n' +
'| Classes | Metaclasses |\n' +
@ -677,6 +692,7 @@ function updateDiagrams() {
$(`code:contains(${DIAGRAM_7_A})`).html(DIAGRAM_7_B);
$(`code:contains(${DIAGRAM_8_A})`).html(DIAGRAM_8_B);
$(`code:contains(${DIAGRAM_9_A})`).html(DIAGRAM_9_B);
$(`code:contains(${DIAGRAM_95_A})`).html(DIAGRAM_95_B);
$(`code:contains(${DIAGRAM_10_A})`).html(DIAGRAM_10_B);
$(`code:contains(${DIAGRAM_11_A})`).html(DIAGRAM_11_B);
$(`code:contains(${DIAGRAM_12_A})`).html(DIAGRAM_12_B).removeClass("text").removeClass("language-text").addClass("python");

3
pdf/index_for_pdf.html

@ -86,8 +86,7 @@
<strong>math module, <a href="#math">7</a></strong><br>
<strong>memoryviews, <a href="#memoryview">29</a></strong><br>
<strong>metaclass attribute, <a href="#metaclassattribute">32</a></strong><br>
<strong>metaprogramming, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong><br>
<strong>mysql library, <a href="#mysql">27</a></strong> </p>
<strong>metaprogramming, <a href="#metaprogramming">31</a>-<a href="#metaclass">32</a></strong> </p>
<h3 id="n">N</h3>
<p><strong>namedtuples, <a href="#namedtuple">3</a></strong><br>
<strong>nonlocal keyword, <a href="#nonlocal">12</a></strong><br>

3
pdf/index_for_pdf_print.html

@ -86,8 +86,7 @@
<strong>math module, 7</strong><br>
<strong>memoryviews, 29</strong><br>
<strong>metaclass attribute, 32</strong><br>
<strong>metaprograming, 31-32</strong><br>
<strong>mysql library, 27</strong> </p>
<strong>metaprograming, 31-32</strong> </p>
<h3 id="n">N</h3>
<p><strong>namedtuples, 3</strong><br>
<strong>nonlocal keyword, 12</strong><br>

1058
web/empty_script.py
File diff suppressed because it is too large
View File

Loading…
Cancel
Save