<div><h2id="sqlite"><ahref="#sqlite"name="sqlite">#</a>SQLite</h2><p><strong>Server-less database engine that stores each database into a separate file.</strong></p><div><h3id="connect">Connect</h3><p><strong>Opens a connection to the database file. Creates a new file if path doesn't exist.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sqlite3
<con> = sqlite3.connect(<path>)<spanclass="hljs-comment"># Also ':memory:'.</span>
<con>.close()<spanclass="hljs-comment"># Closes the connection.</span>
<conn> = sqlite3.connect(<path>) <spanclass="hljs-comment"># Also ':memory:'.</span>
<conn>.close() <spanclass="hljs-comment"># Closes the connection.</span>
</code></pre></div></div>
<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> = <con>.execute(<spanclass="hljs-string">'<query>'</span>)<spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</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> = <conn>.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
<tuple> = <cursor>.fetchone() <spanclass="hljs-comment"># Returns next row. Also next(<cursor>).</span>
<list> = <cursor>.fetchall() <spanclass="hljs-comment"># Returns remaining rows. Also list(<cursor>).</span>
</code></pre></div>
<div><h3id="write-1">Write</h3><pre><codeclass="python language-python hljs"><con>.execute(<spanclass="hljs-string">'<query>'</span>)<spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
<con>.commit()<spanclass="hljs-comment"># Commits all transactions since last commit.</span>
<div><h3id="write-1">Write</h3><pre><codeclass="python language-python hljs"><conn>.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
<conn>.commit() <spanclass="hljs-comment"># Commits all transactions since last commit.</span>
<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 <ahref="#encode">ISO formatted strings</a>.</strong></li>
</ul><pre><codeclass="python language-python hljs"><con>.execute(<spanclass="hljs-string">'<query>'</span>, <list/tuple>)<spanclass="hljs-comment"># Replaces '?'s in query with values.</span>
<con>.execute(<spanclass="hljs-string">'<query>'</span>, <dict/namedtuple>)<spanclass="hljs-comment"># Replaces ':<key>'s with values.</span>
<div><h3id="example">Example</h3><p><strong>In this example values are not actually saved because <codeclass="python hljs"><spanclass="hljs-string">'con.commit()'</span></code> is omitted!</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>con = sqlite3.connect(<spanclass="hljs-string">'test.db'</span>)
<spanclass="hljs-meta">>>></span>con.execute(<spanclass="hljs-string">'create table person (person_id integer primary key, name, height)'</span>)
<spanclass="hljs-meta">>>></span>con.execute(<spanclass="hljs-string">'insert into person values (null, ?, ?)'</span>, (<spanclass="hljs-string">'Jean-Luc'</span>, <spanclass="hljs-number">187</span>)).lastrowid
<div><h3id="example">Example</h3><p><strong>In this example values are not actually saved because <codeclass="python hljs"><spanclass="hljs-string">'conn.commit()'</span></code> is omitted!</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-meta">>>></span>conn = sqlite3.connect(<spanclass="hljs-string">'test.db'</span>)
<spanclass="hljs-meta">>>></span>conn.execute(<spanclass="hljs-string">'create table person (person_id integer primary key, name, height)'</span>)
<spanclass="hljs-meta">>>></span>conn.execute(<spanclass="hljs-string">'insert into person values (null, ?, ?)'</span>, (<spanclass="hljs-string">'Jean-Luc'</span>, <spanclass="hljs-number">187</span>)).lastrowid
<spanclass="hljs-number">1</span>
<spanclass="hljs-meta">>>></span>con.execute(<spanclass="hljs-string">'select * from person'</span>).fetchall()
<spanclass="hljs-meta">>>></span>conn.execute(<spanclass="hljs-string">'select * from person'</span>).fetchall()
<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
<cursor> = <conn>.cursor() <spanclass="hljs-comment"># Only cursor has execute method.</span>
<cursor>.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise a subclass of connector.Error.</span>
<cursor>.execute(<spanclass="hljs-string">'<query>'</span>, <list/tuple>) <spanclass="hljs-comment"># Replaces '%s's in query with values.</span>
<cursor>.execute(<spanclass="hljs-string">'<query>'</span>, <dict/namedtuple>) <spanclass="hljs-comment"># Replaces '%(<key>)s's with values.</span>