<div><h2id="sqlite"><ahref="#sqlite"name="sqlite">#</a>SQLite</h2><p><strong>A 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
<conn> = sqlite3.connect(<path>) <spanclass="hljs-comment"># Also ':memory:'.</span>
<div><h2id="sqlite"><ahref="#sqlite"name="sqlite">#</a>SQLite</h2><p><strong>A server-less database engine that stores each database into a separate file.</strong></p><pre><codeclass="python language-python hljs"><spanclass="hljs-keyword">import</span> sqlite3
<conn> = sqlite3.connect(<path>) <spanclass="hljs-comment"># Opens existing or new file. Also ':memory:'.</span>
<conn>.close() <spanclass="hljs-comment"># Closes the connection.</span>
</code></pre></div></div>
</code></pre></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> = <conn>.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
<div><h3id="read-1">Read</h3><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"><conn>.execute(<spanclass="hljs-string">'<query>'</span>) <spanclass="hljs-comment"># Can raise a subclass of sqlite3.Error.</span>
<conn>.commit() <spanclass="hljs-comment"># Saves all changes since the last commit.</span>
<conn>.rollback() <spanclass="hljs-comment"># Discards all changes since the last commit.</span>
<li><strong>Passed values can be of type str, int, float, bytes, None, bool, datetime.date or datetime.datetime.</strong></li>
<li><strong>Bools will be stored and returned as ints and dates as <ahref="#encode">ISO formatted strings</a>.</strong></li>
</ul>
<div><h3id="example">Example</h3><p><strong>Values are not actually saved in this example 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