1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="./">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sqlite3 module differences — APSW 3.46.0.1 documentation</title>
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=72bcf2f2" />
<link rel="stylesheet" type="text/css" href="_static/css/theme.css?v=19f00094" />
<link rel="stylesheet" type="text/css" href="_static/apsw.css?v=3c7e2631" />
<link rel="shortcut icon" href="_static/favicon.ico"/>
<!--[if lt IE 9]>
<script src="_static/js/html5shiv.min.js"></script>
<![endif]-->
<script src="_static/jquery.js?v=5d32c60e"></script>
<script src="_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="_static/documentation_options.js?v=d98f5d2b"></script>
<script src="_static/doctools.js?v=9a2dae69"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="_static/js/theme.js"></script>
<link rel="author" title="About these documents" href="about.html" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="next" title="Benchmarking" href="benchmarking.html" />
<link rel="prev" title="DBAPI notes" href="dbapi.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="index.html" class="icon icon-home">
APSW
<img src="_static/apswlogo.png" class="logo" alt="Logo"/>
</a>
<div class="version">
3.46.0.1
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a></li>
<li class="toctree-l1"><a class="reference internal" href="tips.html">Tips</a></li>
<li class="toctree-l1"><a class="reference internal" href="example.html">Example/Tour</a></li>
<li class="toctree-l1"><a class="reference internal" href="install.html">Installation and customization</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">Extensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="apsw.html">APSW Module</a></li>
<li class="toctree-l1"><a class="reference internal" href="connection.html">Connections to a database</a></li>
<li class="toctree-l1"><a class="reference internal" href="cursor.html">Cursors (executing SQL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="blob.html">Blob Input/Output</a></li>
<li class="toctree-l1"><a class="reference internal" href="backup.html">Backup</a></li>
<li class="toctree-l1"><a class="reference internal" href="vtable.html">Virtual Tables</a></li>
<li class="toctree-l1"><a class="reference internal" href="vfs.html">Virtual File System (VFS)</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="bestpractice.html">Best Practice</a></li>
<li class="toctree-l1"><a class="reference internal" href="ext.html">Various interesting and useful bits of functionality</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">Exceptions and Errors</a></li>
<li class="toctree-l1"><a class="reference internal" href="execution.html">Execution and tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="dbapi.html">DBAPI notes</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">sqlite3 module differences</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#what-apsw-does-better">What APSW does better</a></li>
<li class="toctree-l2"><a class="reference internal" href="#what-sqlite3-does-better">What sqlite3 does better</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">Benchmarking</a></li>
<li class="toctree-l1"><a class="reference internal" href="copyright.html">Copyright and License</a></li>
<li class="toctree-l1"><a class="reference internal" href="changes.html">Change History</a></li>
<li class="toctree-l1"><a class="reference internal" href="py-modindex.html">Module Index</a></li>
<li class="toctree-l1"><a class="reference internal" href="genindex.html">Index</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">APSW</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item active">sqlite3 module differences</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/pysqlite.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="dbapi.html" class="btn btn-neutral float-left" title="DBAPI notes" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="benchmarking.html" class="btn btn-neutral float-right" title="Benchmarking" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="sqlite3-module-differences">
<span id="pysqlitediffs"></span><h1>sqlite3 module differences<a class="headerlink" href="#sqlite3-module-differences" title="Link to this heading"></a></h1>
<p>The <a class="reference external" href="https://docs.python.org/3/library/sqlite3.html">sqlite3</a>
standard module and APSW approached the problem of providing access
to SQLite from Python from fundamentally different directions.</p>
<p>APSW provides access in whatever way is normal for SQLite. It makes
no effort to hide how SQLite is different from other databases.</p>
<p>sqlite3 tries to provide a DBAPI (<span class="target" id="index-0"></span><a class="pep reference external" href="https://peps.python.org/pep-0249/"><strong>PEP 249</strong></a>) compliant wrapper for
SQLite and in doing so needs to make it have the same behaviour as
other databases. Consequently it does hide some of SQLite’s nuances.</p>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>I suggest using APSW when you want to directly use SQLite and its
functionality or are using your own code to deal with database
independence rather than DBAPI. Use <a class="reference external" href="https://docs.python.org/3/library/sqlite3.html#module-sqlite3" title="(in Python v3.12)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">sqlite3</span></code></a> and DBAPI if
your needs are simple, and you don’t want to use SQLite specific
features, and want to easily switch to other databases.</p>
</div>
<section id="what-apsw-does-better">
<h2>What APSW does better<a class="headerlink" href="#what-apsw-does-better" title="Link to this heading"></a></h2>
<p>APSW has the following enhancements/differences over the sqlite3
module:</p>
<ul>
<li><p>APSW stays up to date with SQLite. As features are added and
functionality changed in SQLite, APSW tracks them.</p></li>
<li><p>APSW stays up to date with Python, including releases under
development as well as older still supported releases. The current
APSW release brings the most recent SQLite to Python 3.8 all the way
through Python 3.13.</p></li>
<li><p>APSW gives all functionality of SQLite including <a class="reference internal" href="vtable.html#virtualtables"><span class="std std-ref">virtual
tables</span></a>, <a class="reference internal" href="vfs.html#vfs"><span class="std std-ref">Virtual File System (VFS)</span></a>, <a class="reference internal" href="blob.html#blobio"><span class="std std-ref">BLOB I/O</span></a>,
<a class="reference internal" href="backup.html#backup"><span class="std std-ref">backups</span></a>, <a class="reference internal" href="ext.html#apsw.ext.log_sqlite" title="apsw.ext.log_sqlite"><code class="xref py py-meth docutils literal notranslate"><span class="pre">logging</span></code></a>, and
<a class="reference internal" href="connection.html#apsw.Connection.file_control" title="apsw.Connection.file_control"><code class="xref py py-meth docutils literal notranslate"><span class="pre">file</span> <span class="pre">control</span></code></a>.</p></li>
<li><p>APSW includes <a class="reference internal" href="bestpractice.html#module-apsw.bestpractice" title="apsw.bestpractice: Ensure SQLite usage prevents common mistakes, and get best performance"><code class="xref py py-mod docutils literal notranslate"><span class="pre">apsw.bestpractice</span></code></a> which configures SQLite
for best performance, and catches common mistakes.</p></li>
<li><p>You can use the same <a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> across threads with APSW
without needing any additional level of locking. sqlite3 <a class="reference external" href="https://docs.python.org/3/library/sqlite3.html?highlight=sqlite#sqlite3.threadsafety">requires</a>
that the <a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> and any <a class="reference internal" href="cursor.html#apsw.Cursor" title="apsw.Cursor"><code class="xref py py-class docutils literal notranslate"><span class="pre">cursors</span></code></a> are
used in the same thread.</p></li>
<li><p>APSW on <a class="reference internal" href="install.html#pypi"><span class="std std-ref">PyPI</span></a> includes SQLite statically inside which
does not interfere with any system SQLite, so you have the latest
SQLite without disruptions.</p></li>
<li><p>If you don’t use PyPI, APSW <a class="reference external" href="https://docs.python.org/3/extending/building.html#building" title="(in Python v3.12)"><span class="xref std std-ref">build instructions</span></a>
include extensive customisation options for SQLite.</p></li>
<li><p><strong>Nothing</strong> happens behind your back. By default sqlite3 tries to
manage transactions (for DBAPI compliance) by processing your SQL for
you, but you can turn it off. This can result in very unexpected
behaviour with sqlite3.</p></li>
<li><p>When using a <a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal notranslate"><span class="pre">Connection</span></code></a> as a <a class="reference internal" href="connection.html#apsw.Connection.__enter__" title="apsw.Connection.__enter__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">context</span> <span class="pre">manager</span></code></a> APSW uses SQLite’s ability to have <a class="reference external" href="https://sqlite.org/lang_savepoint.html">nested
transactions</a>. sqlite3
has a context manager, but does not implement nesting.</p></li>
<li><p>You can use semi-colons at the end of commands and you can have
multiple commands in the execute string in APSW. There are no
restrictions on the type of commands used. For example this will
work fine in APSW but is not allowed in sqlite3:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">apsw</span>
<span class="n">con</span><span class="o">=</span><span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">(</span><span class="s2">":memory:"</span><span class="p">)</span>
<span class="n">cur</span><span class="o">=</span><span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"create table foo(x,y,z);insert into foo values (?,?,?);"</span>
<span class="s2">"insert into foo values(?,?,?);select * from foo;drop table foo;"</span>
<span class="s2">"create table bar(x,y);insert into bar values(?,?);"</span>
<span class="s2">"insert into bar values(?,?);select * from bar;"</span><span class="p">,</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">10</span><span class="p">)):</span>
<span class="nb">print</span> <span class="p">(</span><span class="n">row</span><span class="p">)</span>
</pre></div>
</div>
<p>And the output as you would expect:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">)</span>
<span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">)</span>
<span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
</li>
<li><p><a class="reference internal" href="cursor.html#apsw.Cursor.executemany" title="apsw.Cursor.executemany"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Cursor.executemany()</span></code></a> also works with statements that return
data such as selects, and you can have multiple statements.
sqlite3’s <em>executescript</em> method doesn’t allow any form of
data being returned (it silently ignores any returned data).</p></li>
<li><p>APSW has better <a class="reference internal" href="execution.html#tracing"><span class="std std-ref">execution and row tracing</span></a>.
<a class="reference internal" href="ext.html"><span class="doc">Various interesting and useful bits of functionality</span></a> provides accessing rows by column name, adapting and
converting types going into and coming out of SQLite, getting query
details etc.</p></li>
<li><p>sqlite3 swallows exceptions in your callbacks making it far harder
to debug problems. That also prevents you from raising exceptions in
your callbacks to be handled in your code that called SQLite.
sqlite3 does let you turn on <a class="reference external" href="https://docs.python.org/3/library/sqlite3.html?highlight=sqlite#sqlite3.enable_callback_tracebacks">printing of tracebacks</a>
but that is a poor substitute.</p>
<p>APSW does the right thing as demonstrated by this example. APSW
converts Python errors into SQLite errors, so SQLite is aware errors
happened.</p>
<p>Source:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">badfunc</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
<span class="k">return</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
<span class="c1"># sqlite3</span>
<span class="kn">import</span> <span class="nn">sqlite3</span>
<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s2">":memory:"</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">create_function</span><span class="p">(</span><span class="s2">"badfunc"</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">badfunc</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"select badfunc(3)"</span><span class="p">)</span>
<span class="c1"># apsw</span>
<span class="kn">import</span> <span class="nn">apsw</span>
<span class="n">con</span> <span class="o">=</span> <span class="n">apsw</span><span class="o">.</span><span class="n">Connection</span><span class="p">(</span><span class="s2">":memory:"</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">create_scalar_function</span><span class="p">(</span><span class="s2">"badfunc"</span><span class="p">,</span> <span class="n">badfunc</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s2">"select badfunc(3)"</span><span class="p">)</span>
</pre></div>
</div>
<p>Exceptions:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span># sqlite3
Traceback (most recent call last):
File "func.py", line 8, in ?
cur.execute("select badfunc(3)")
sqlite3.OperationalError: user-defined function raised exception
# apsw
Traceback (most recent call last):
File "t.py", line 8, in ?
cur.execute("select badfunc(3)")
File "apsw.c", line 3660, in resetcursor
File "apsw.c", line 1871, in user-defined-scalar-badfunc
File "t.py", line 3, in badfunc
return 1/0
</pre></div>
</div>
</li>
<li><p>APSW has significantly enhanced debuggability. More details are
available than just what is printed out when exceptions happen like
above. See <a class="reference internal" href="exceptions.html#augmentedstacktraces"><span class="std std-ref">augmented stack traces</span></a></p></li>
<li><p>APSW has an <a class="reference internal" href="execution.html#apswtrace"><span class="std std-ref">trace</span></a> utility script that traces
execution and results in your code without having to modify it in
any way. It also outputs summary reports making it easy to see what
your most time consuming queries are, which are most popular etc.</p></li>
<li><p>APSW has an exception corresponding to each SQLite error code and
provides the extended error code. sqlite3 combines several SQLite
error codes into corresponding DBAPI exceptions. This is a good
example of the difference in approach of the two wrappers.</p></li>
<li><p>The APSW test suite is larger and tests more functionality. Virtually every
failure condition is tested including running out of memory, error
returns etc. Code coverage by the test suite is 99.6%. sqlite3 is
good at 81% for C code although there are several places that
coverage can be improved. I haven’t measured code coverage for
sqlite3’s Python code. The consequences of this are that APSW
catches issues earlier and gives far better diagnostics. As an
example try returning an unsupported type from a registered scalar
function.</p></li>
<li><p>APSW is faster than sqlite3 in my testing. Try the
<a class="reference internal" href="benchmarking.html#speedtest"><span class="std std-ref">speedtest</span></a> benchmark.</p></li>
</ul>
</section>
<section id="what-sqlite3-does-better">
<h2>What sqlite3 does better<a class="headerlink" href="#what-sqlite3-does-better" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>sqlite3 is part of the standard library, and is widely supported by
libraries that abstract away the database layer</p></li>
</ul>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="dbapi.html" class="btn btn-neutral float-left" title="DBAPI notes" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="benchmarking.html" class="btn btn-neutral float-right" title="Benchmarking" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© <a href="copyright.html">Copyright</a> 2004-2024, Roger Binns <rogerb@rogerbinns.com>.
<span class="lastupdated">Last updated on Jun 16, 2024.
</span></p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>
|