/usr/share/doc/python-apsw/html/tips.html is in python-apsw-doc 3.13.0-r1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Tips — APSW 3.13.0-r1 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '3.13.0-r1',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="copyright" title="Copyright" href="copyright.html" />
<link rel="top" title="APSW 3.13.0-r1 documentation" href="index.html" />
<link rel="next" title="Example" href="example.html" />
<link rel="prev" title="APSW documentation" href="index.html" />
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-26815066-1']);
_gaq.push(['_trackPageview']);
</script>
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="example.html" title="Example"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="APSW documentation"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">APSW 3.13.0-r1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="tips">
<h1>Tips<a class="headerlink" href="#tips" title="Permalink to this headline">¶</a></h1>
<p>These tips are based on mailing list postings. You are recommended to
read all the documentation as well.</p>
<div class="section" id="sqlite-is-different">
<h2>SQLite is different<a class="headerlink" href="#sqlite-is-different" title="Permalink to this headline">¶</a></h2>
<p>While SQLite provides a SQL database like many others out there, it is
also unique in many ways. Read about the unique features at the
<a class="reference external" href="https://sqlite.org/different.html">SQLite website</a>.</p>
</div>
<div class="section" id="cursors">
<h2>Cursors<a class="headerlink" href="#cursors" title="Permalink to this headline">¶</a></h2>
<p>SQLite only calculates each result row as you request it. For example
if your query returns 10 million rows SQLite will not calculate all 10
million up front. Instead the next row will be calculated as you ask
for it.</p>
<p>Cursors on the same <a class="reference internal" href="connection.html#connections"><span>Connection</span></a> are not isolated
from each other. Anything done on one cursor is immediately visible
to all other Cursors on the same connection. This still applies if
you start transactions. Connections are isolated from each other.</p>
<p>Read more about <a class="reference internal" href="cursor.html#cursors"><span>Cursors</span></a>.</p>
</div>
<div class="section" id="bindings">
<h2>Bindings<a class="headerlink" href="#bindings" title="Permalink to this headline">¶</a></h2>
<p>When using a cursor, always use bindings. <a class="reference external" href="http://docs.python.org/library/stdtypes.html#string-formatting-operations">String interpolation</a>
may seem more convenient but you will encounter difficulties. You may
feel that you have complete control over all data accessed but if your
code is at all useful then you will find it being used more and more
widely. The computer will always be better than you at parsing SQL
and the bad guys have years of experience finding and using <a class="reference external" href="http://en.wikipedia.org/wiki/SQL_injection">SQL
injection attacks</a> in
ways you never even thought possible.</p>
<p>The <a class="reference internal" href="cursor.html#cursors"><span>documentation</span></a> gives many examples of how to use
various forms of bindings.</p>
</div>
<div class="section" id="unicode">
<h2>Unicode<a class="headerlink" href="#unicode" title="Permalink to this headline">¶</a></h2>
<p>SQLite only stores text as Unicode. However it relies on SQLite API
users to provide valid UTF-8 and does not double check. (APSW only
provides valid UTF-8). It is possible using other wrappers and tools
to cause invalid UTF-8 to appear in the database which will then cause
retrieval errors. You can work around this by using the SQL <em>CAST</em>
operator. For example:</p>
<div class="highlight-python"><div class="highlight"><pre>SELECT id, CAST(label AS blob) from table
</pre></div>
</div>
<p>Then proceed to give the <a class="reference external" href="http://www.joelonsoftware.com/articles/Unicode.html">Joel Unicode article</a> to all people
involved.</p>
</div>
<div class="section" id="diagnostics">
<span id="diagnostics-tips"></span><h2>Diagnostics<a class="headerlink" href="#diagnostics" title="Permalink to this headline">¶</a></h2>
<p>Both SQLite and APSW provide detailed diagnostic information. Errors
will be signalled via an <a class="reference internal" href="exceptions.html"><em>exception</em></a>.</p>
<p>APSW ensures you have <a class="reference internal" href="exceptions.html#augmentedstacktraces"><span>detailed information</span></a> both in the stack trace as well as what data
APSW/SQLite was operating on.</p>
<p>SQLite has a <a class="reference external" href="http://www.sqlite.org/errlog.html">warning/error logging facility</a>. To set your own logger use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">handler</span><span class="p">(</span><span class="n">errcode</span><span class="p">,</span> <span class="n">message</span><span class="p">):</span>
<span class="n">errstr</span><span class="o">=</span><span class="n">apsw</span><span class="o">.</span><span class="n">mapping_result_codes</span><span class="p">[</span><span class="n">errcode</span> <span class="o">&</span> <span class="mi">255</span><span class="p">]</span>
<span class="n">extended</span><span class="o">=</span><span class="n">errcode</span> <span class="o">&</span> <span class="o">~</span> <span class="mi">255</span>
<span class="k">print</span> <span class="s">"SQLITE_LOG: </span><span class="si">%s</span><span class="s"> (</span><span class="si">%d</span><span class="s">) </span><span class="si">%s</span><span class="s"> </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="n">errcode</span><span class="p">,</span> <span class="n">errstr</span><span class="p">,</span> <span class="n">apsw</span><span class="o">.</span><span class="n">mapping_extended_result_codes</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">extended</span><span class="p">,</span> <span class="s">""</span><span class="p">))</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">config</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_CONFIG_LOG</span><span class="p">,</span> <span class="n">handler</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The handler <strong>must</strong> be set before any other calls to SQLite.
Once SQLite is initialised you cannot change the logger - a
<a class="reference internal" href="exceptions.html#apsw.MisuseError" title="apsw.MisuseError"><code class="xref py py-exc docutils literal"><span class="pre">MisuseError</span></code></a> will happen (this restriction is in SQLite not
APSW).</p>
</div>
<p>This is an example of what gets printed when I use <code class="docutils literal"><span class="pre">/dev/null</span></code> as
the database name in the <a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal"><span class="pre">Connection</span></code></a> and then tried to create
a table.:</p>
<div class="highlight-python"><div class="highlight"><pre>SQLITE_LOG: cannot open file at line 28729 of [7dd4968f23] (14) SQLITE_CANTOPEN
SQLITE_LOG: os_unix.c:28729: (2) open(/dev/null-journal) - No such file or directory (14) SQLITE_CANTOPEN
SQLITE_LOG: statement aborts at 38: [create table foo(x,y);] unable to open database file (14) SQLITE_CANTOPEN
</pre></div>
</div>
</div>
<div class="section" id="parsing-sql">
<h2>Parsing SQL<a class="headerlink" href="#parsing-sql" title="Permalink to this headline">¶</a></h2>
<p>Sometimes you want to know what a particular SQL statement does. The
SQLite query parser directly generates VDBE byte code and cannot be
hooked into. There is however an easier way.</p>
<p>Make a new <a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal"><span class="pre">Connection</span></code></a> object making sure the statement cache
is disabled (size zero). Install an <a class="reference internal" href="execution.html#executiontracer"><span>execution tracer</span></a> that returns <code class="docutils literal"><span class="pre">apsw.SQLITE_DENY</span></code> which will
prevent any queries from running. Install an <a class="reference internal" href="connection.html#apsw.Connection.setauthorizer" title="apsw.Connection.setauthorizer"><code class="xref py py-meth docutils literal"><span class="pre">authorizer</span></code></a>.</p>
<p>Then call <a class="reference internal" href="cursor.html#apsw.Cursor.execute" title="apsw.Cursor.execute"><code class="xref py py-meth docutils literal"><span class="pre">Cursor.execute()</span></code></a> on your query. Your authorizer will
then be called (multiple times if necessary) with details of what the
query does including expanding views and triggers that fire. Finally
the execution tracer will fire. If the query string had multiple
statements then the execution tracer lets you know how long the first
statement was.</p>
</div>
<div class="section" id="unexpected-behaviour">
<h2>Unexpected behaviour<a class="headerlink" href="#unexpected-behaviour" title="Permalink to this headline">¶</a></h2>
<p>Occasionally you may get different results than you expected. Before
littering your code with <em>print</em>, try <a class="reference internal" href="execution.html#apswtrace"><span>apswtrace</span></a>
with all options turned on to see exactly what is going on. You can
also use the <a class="reference internal" href="shell.html#shell"><span>SQLite shell</span></a> to dump the contents of your
database to a text file. For example you could dump it before and
after a run to see what changed.</p>
<p>One fairly common gotcha is using double quotes instead of single
quotes. (This wouldn’t be a problem if you use bindings!) SQL
strings use single quotes. If you use double quotes then it will
mostly appear to work, but they are intended to be used for
identifiers such as column names. For example if you have a column
named <code class="docutils literal"><span class="pre">a</span> <span class="pre">b</span></code> (a space b) then you would need to use:</p>
<div class="highlight-python"><div class="highlight"><pre>SELECT "a b" from table
</pre></div>
</div>
<p>If you use double quotes and happen to use a string whose contents are
the same as a table, alias, column etc then unexpected results will
occur.</p>
</div>
<div class="section" id="customizing-cursors">
<h2>Customizing cursors<a class="headerlink" href="#customizing-cursors" title="Permalink to this headline">¶</a></h2>
<p>Some developers want to customize the behaviour of cursors. An
example would be wanting a <a class="reference internal" href="dbapi.html#rowcount"><span>rowcount</span></a> or batching returned rows.
(These don’t make any sense with SQLite but the desire may be to make
the code source compatible with other database drivers).</p>
<p>APSW does not provide a way to subclass the cursor class or any other
form of factory. Consequently you will have to subclass the
<a class="reference internal" href="connection.html#apsw.Connection" title="apsw.Connection"><code class="xref py py-class docutils literal"><span class="pre">Connection</span></code></a> and provide an alternate implementation of
<a class="reference internal" href="connection.html#apsw.Connection.cursor" title="apsw.Connection.cursor"><code class="xref py py-meth docutils literal"><span class="pre">Connection.cursor()</span></code></a>. You should encapsulate the APSW cursor -
ie store it as a member of your cursor class and forward calls as
appropriate. The cursor only has two important methods -
<a class="reference internal" href="cursor.html#apsw.Cursor.execute" title="apsw.Cursor.execute"><code class="xref py py-meth docutils literal"><span class="pre">Cursor.execute()</span></code></a> and <a class="reference internal" href="cursor.html#apsw.Cursor.executemany" title="apsw.Cursor.executemany"><code class="xref py py-meth docutils literal"><span class="pre">Cursor.executemany()</span></code></a>.</p>
<p>If you want to change the rows returned then use a <a class="reference internal" href="execution.html#rowtracer"><span>row tracer</span></a>. For example you could call
<a class="reference internal" href="cursor.html#apsw.Cursor.getdescription" title="apsw.Cursor.getdescription"><code class="xref py py-meth docutils literal"><span class="pre">Cursor.getdescription()</span></code></a> and return a dictionary instead of a
tuple:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">row_factory</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">row</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span><span class="n">k</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span> <span class="n">row</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">cursor</span><span class="o">.</span><span class="n">getdescription</span><span class="p">())}</span>
<span class="c"># You can also set this on just a cursor</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setrowtrace</span><span class="p">(</span><span class="n">row_factory</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="busy-handling">
<span id="busyhandling"></span><h2>Busy handling<a class="headerlink" href="#busy-handling" title="Permalink to this headline">¶</a></h2>
<p>SQLite uses locks to coordinate access to the database by multiple
connections (within the same process or in a different process). The
general goal is to have the locks be as lax as possible (allowing
concurrency) and when using more restrictive locks to keep them for as
short a time as possible. See the <a class="reference external" href="https://sqlite.org/lockingv3.html">SQLite documentation</a> for more details.</p>
<p>By default you will get a <a class="reference internal" href="exceptions.html#apsw.BusyError" title="apsw.BusyError"><code class="xref py py-exc docutils literal"><span class="pre">BusyError</span></code></a> if a lock cannot be
acquired. You can set a <a class="reference internal" href="connection.html#apsw.Connection.setbusytimeout" title="apsw.Connection.setbusytimeout"><code class="xref py py-meth docutils literal"><span class="pre">timeout</span></code></a>
which will keep retrying or a <a class="reference internal" href="connection.html#apsw.Connection.setbusyhandler" title="apsw.Connection.setbusyhandler"><code class="xref py py-meth docutils literal"><span class="pre">callback</span></code></a> where you decide what to do.</p>
</div>
<div class="section" id="database-schema">
<h2>Database schema<a class="headerlink" href="#database-schema" title="Permalink to this headline">¶</a></h2>
<p>When starting a new database, it can be quite difficult to decide what
tables and fields to have and how to link them. The technique used to
design SQL schemas is called <a class="reference external" href="http://en.wikipedia.org/wiki/Database_normalization">normalization</a>. The page
also shows common pitfalls if you don’t normalize your schema.</p>
</div>
<div class="section" id="shared-cache-mode">
<span id="sharedcache"></span><h2>Shared Cache Mode<a class="headerlink" href="#shared-cache-mode" title="Permalink to this headline">¶</a></h2>
<p>SQLite supports a <a class="reference external" href="https://sqlite.org/sharedcache.html">shared cache mode</a> where multiple connections
to the same database can share a cache instead of having their own.
It is not recommended that you use this mode.</p>
<p>A big issue is that <a class="reference internal" href="#busyhandling"><span>busy handling</span></a> is not done
the same way. The timeouts and handlers are ignored and instead
<code class="xref py py-const docutils literal"><span class="pre">SQLITE_LOCKED_SHAREDCACHE</span></code> extended error is returned.
Consequently you will have to do your own busy handling. (<a class="reference external" href="https://sqlite.org/src/tktview/ebde3f66fc64e21e61ef2854ed1a36dfff884a2f">SQLite
ticket</a>,
<a class="reference external" href="https://github.com/rogerbinns/apsw/issues/59">APSW issue 59</a>)</p>
<p>The amount of memory and I/O saved is trivial compared to Python’s
overal memory and I/O consumption. You may also need to tune the
shared cache’s memory back up to what it would have been with seperate
connections to get the same performance.</p>
<p>The shared cache mode is targetted at embedded systems where every
byte of memory and I/O matters. For example an MP3 player may only
have kilobytes of memory available for SQLite.</p>
</div>
<div class="section" id="write-ahead-logging">
<span id="wal"></span><h2>Write Ahead Logging<a class="headerlink" href="#write-ahead-logging" title="Permalink to this headline">¶</a></h2>
<p>SQLite 3.7 introduces <a class="reference external" href="https://sqlite.org/wal.html">write ahead logging</a> which has several benefits, but
also some drawbacks as the page documents. WAL mode is off by
default. In addition to turning it on manually for each database, you
can also turn it on for all opened databases by using
<a class="reference internal" href="apsw.html#apsw.connection_hooks" title="apsw.connection_hooks"><code class="xref py py-attr docutils literal"><span class="pre">connection_hooks</span></code></a>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">setwal</span><span class="p">(</span><span class="n">db</span><span class="p">):</span>
<span class="n">db</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"pragma journal_mode=wal"</span><span class="p">)</span>
<span class="c"># custom auto checkpoint interval (use zero to disable)</span>
<span class="n">db</span><span class="o">.</span><span class="n">wal_autocheckpoint</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">connection_hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">setwal</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that if wal mode can’t be set (eg the database is in memory or
temporary) then the attempt to set wal mode will be ignored. The
pragma will return the mode in effect. It is also harmless to call
functions like <a class="reference internal" href="connection.html#apsw.Connection.wal_autocheckpoint" title="apsw.Connection.wal_autocheckpoint"><code class="xref py py-meth docutils literal"><span class="pre">Connection.wal_autocheckpoint()</span></code></a> on connections
that are not in wal mode.</p>
<p>If you write your own VFS, then inheriting from an existing VFS that
supports WAL will make your VFS support the extra WAL methods too.
(Your VFS will point directly to the base methods - there is no
indirect call via Python.)</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Tips</a><ul>
<li><a class="reference internal" href="#sqlite-is-different">SQLite is different</a></li>
<li><a class="reference internal" href="#cursors">Cursors</a></li>
<li><a class="reference internal" href="#bindings">Bindings</a></li>
<li><a class="reference internal" href="#unicode">Unicode</a></li>
<li><a class="reference internal" href="#diagnostics">Diagnostics</a></li>
<li><a class="reference internal" href="#parsing-sql">Parsing SQL</a></li>
<li><a class="reference internal" href="#unexpected-behaviour">Unexpected behaviour</a></li>
<li><a class="reference internal" href="#customizing-cursors">Customizing cursors</a></li>
<li><a class="reference internal" href="#busy-handling">Busy handling</a></li>
<li><a class="reference internal" href="#database-schema">Database schema</a></li>
<li><a class="reference internal" href="#shared-cache-mode">Shared Cache Mode</a></li>
<li><a class="reference internal" href="#write-ahead-logging">Write Ahead Logging</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter">APSW documentation</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="example.html"
title="next chapter">Example</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/tips.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="example.html" title="Example"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="APSW documentation"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">APSW 3.13.0-r1 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© <a href="copyright.html">Copyright</a> 2004-2016, Roger Binns <rogerb@rogerbinns.com>.
Last updated on Jun 17, 2016.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.6.
</div>
</body>
</html>
|