<div><h2id="input"><ahref="#input"name="input">#</a>Input</h2><p><strong>Reads a line from user input or pipe if present.</strong></p><pre><codeclass="python language-python hljs"><str> = input(prompt=<spanclass="hljs-keyword">None</span>)
<div><h2id="sqlite"><ahref="#sqlite"name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into separate file.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sqlite3
<div><h2id="sqlite"><ahref="#sqlite"name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into separate file.</strong></p><div><h3id="connect">Connect</h3><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sqlite3
db = sqlite3.connect(<spanclass="hljs-string">'<path>'</span>) <spanclass="hljs-comment"># Also ':memory:'.</span>
...
db.close()
</code></pre></div>
</code></pre></div></div>
<ul>
<li><strong>New database will be created if path doesn't exist.</strong></li>
</ul>
<div><h3id="read-1">Read</h3><pre><codeclass="python language-python hljs"><cursor> = db.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise sqlite3.OperationalError.</span>
<div><h3id="read-1">Read</h3><p><strong>Returned values can be of type str, int, float, bytes or None.</strong></p><pre><codeclass="python language-python hljs"><cursor> = db.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise sqlite3.OperationalError.</span>
<tuple> = <cursor>.fetchone() <spanclass="hljs-comment"># Returns next row. Also next(<cursor>).</span>
<div><h3id="example">Example</h3><p><strong>In this example values are not actually saved because <codeclass="python hljs"><spanclass="hljs-string">'db.commit()'</span></code> is omitted!</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>db = sqlite3.connect(<spanclass="hljs-string">'test.db'</span>)
<spanclass="hljs-meta">>>></span>db.execute(<spanclass="hljs-string">'create table t (a, b, c)'</span>)
<spanclass="hljs-meta">>>></span>db.execute(<spanclass="hljs-string">'insert into t values (1, 2, 3)'</span>)
<spanclass="hljs-meta">>>></span>db.execute(<spanclass="hljs-string">'select * from t'</span>).fetchall()
<li><strong>In this example values are not actually saved because <codeclass="python hljs"><spanclass="hljs-string">'db.commit()'</span></code> was omitted.</strong></li>
</ul>
<div><h3id="mysql">MySQL</h3><p><strong>Has a very similar interface, with differences listed below.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-comment"># $ pip3 install mysql-connector</span>
<spanclass="hljs-keyword">from</span> mysql <spanclass="hljs-keyword">import</span> connector
db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>)