/usr/share/doc/python-apsw/html/example.html is in python-apsw-doc 3.7.6.3-r1-1build1.
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 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 | <!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>Example — APSW 3.7.6.3-r1 documentation</title>
<link rel="stylesheet" href="_static/default.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.7.6.3-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.7.6.3-r1 documentation" href="index.html" />
<link rel="next" title="Download" href="download.html" />
<link rel="prev" title="Tips" href="tips.html" />
</head>
<body>
<div class="related">
<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="download.html" title="Download"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tips.html" title="Tips"
accesskey="P">previous</a> |</li>
<li><a href="index.html">APSW 3.7.6.3-r1 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="example">
<h1>Example<a class="headerlink" href="#example" title="Permalink to this headline">ΒΆ</a></h1>
<p>This code demonstrates usage of the APSW api. It gives you a good
overview of all the things that can be done. Also included is output
so you can see what gets printed when you run the code.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">os</span><span class="o">,</span> <span class="nn">sys</span><span class="o">,</span> <span class="nn">time</span>
<span class="kn">import</span> <span class="nn">apsw</span>
<span class="c">###</span>
<span class="c">### Check we have the expected version of apsw and sqlite</span>
<span class="c">###</span>
<span class="k">print</span> <span class="s">" Using APSW file"</span><span class="p">,</span><span class="n">apsw</span><span class="o">.</span><span class="n">__file__</span> <span class="c"># from the extension module</span>
<span class="k">print</span> <span class="s">" APSW version"</span><span class="p">,</span><span class="n">apsw</span><span class="o">.</span><span class="n">apswversion</span><span class="p">()</span> <span class="c"># from the extension module</span>
<span class="k">print</span> <span class="s">" SQLite lib version"</span><span class="p">,</span><span class="n">apsw</span><span class="o">.</span><span class="n">sqlitelibversion</span><span class="p">()</span> <span class="c"># from the sqlite library code</span>
<span class="k">print</span> <span class="s">"SQLite header version"</span><span class="p">,</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_VERSION_NUMBER</span> <span class="c"># from the sqlite header file at compile time</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| Using APSW file /space/apsw/apsw.so
| APSW version 3.7.6.3-r1
| SQLite lib version 3.7.6.3
| SQLite header version 3007006
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Opening/creating database</span>
<span class="c">###</span>
<span class="n">connection</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="s">"dbfile"</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">=</span><span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python" id="example-cursor"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### simple statement</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"create table foo(x,y,z)"</span><span class="p">)</span>
<span class="c">###</span>
<span class="c">### using different types</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo values(?,?,?)"</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mf">1.1</span><span class="p">,</span> <span class="bp">None</span><span class="p">))</span> <span class="c"># integer, float/real, Null</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo(x) values(?)"</span><span class="p">,</span> <span class="p">(</span><span class="s">"abc"</span><span class="p">,</span> <span class="p">))</span> <span class="c"># string (note trailing comma to ensure tuple!)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo(x) values(?)"</span><span class="p">,</span> <span class="c"># a blob (binary data)</span>
<span class="p">(</span><span class="nb">buffer</span><span class="p">(</span><span class="s">"abc</span><span class="se">\xff\xfe</span><span class="s">"</span><span class="p">),</span> <span class="p">))</span> <span class="c"># Use b"abc\xff\xfe" for Python 3</span>
<span class="c">###</span>
<span class="c">### multiple statements</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"delete from foo; insert into foo values(1,2,3); create table bar(a,b,c) ; insert into foo values(4, 'five', 6.0)"</span><span class="p">)</span>
<span class="c">###</span>
<span class="c">### iterator</span>
<span class="c">###</span>
<span class="k">for</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select x,y,z from foo"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">cursor</span><span class="o">.</span><span class="n">getdescription</span><span class="p">()</span> <span class="c"># shows column names and declared types</span>
<span class="k">print</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">z</span>
<span class="c">###</span>
<span class="c">### iterator - multiple statements</span>
<span class="c">###</span>
<span class="k">for</span> <span class="n">m</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">o</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select x,y,z from foo ; select a,b,c from bar"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">m</span><span class="p">,</span><span class="n">n</span><span class="p">,</span><span class="n">o</span>
<span class="c">###</span>
<span class="c">### bindings - sequence</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo values(?,?,?)"</span><span class="p">,</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="s">'eight'</span><span class="p">,</span> <span class="bp">False</span><span class="p">))</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo values(?,?,?1)"</span><span class="p">,</span> <span class="p">(</span><span class="s">'one'</span><span class="p">,</span> <span class="s">'two'</span><span class="p">))</span> <span class="c"># nb sqlite does the numbers from 1</span>
<span class="c">###</span>
<span class="c">### bindings - dictionary</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into foo values(:alpha, :beta, :gamma)"</span><span class="p">,</span> <span class="p">{</span><span class="s">'alpha'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s">'beta'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s">'gamma'</span><span class="p">:</span> <span class="s">'three'</span><span class="p">})</span>
</pre></div>
</div>
<div class="highlight-python" id="example-exectrace"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### tracing execution</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">mytrace</span><span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">statement</span><span class="p">,</span> <span class="n">bindings</span><span class="p">):</span>
<span class="s">"Called just before executing each statement"</span>
<span class="k">print</span> <span class="s">"SQL:"</span><span class="p">,</span><span class="n">statement</span>
<span class="k">if</span> <span class="n">bindings</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"Bindings:"</span><span class="p">,</span><span class="n">bindings</span>
<span class="k">return</span> <span class="bp">True</span> <span class="c"># if you return False then execution is aborted</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">setexectrace</span><span class="p">(</span><span class="n">mytrace</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"drop table bar ; create table bar(x,y,z); select * from foo where x=?"</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,))</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| SQL: drop table bar ;
| SQL: create table bar(x,y,z);
| SQL: select * from foo where x=?
| Bindings: (3,)
</pre></div>
</div>
<div class="highlight-python" id="example-rowtrace"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### tracing results</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">rowtrace</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="sd">"""Called with each row of results before they are handed off. You can return None to</span>
<span class="sd"> cause the row to be skipped or a different set of values to return"""</span>
<span class="k">print</span> <span class="s">"Row:"</span><span class="p">,</span> <span class="n">row</span>
<span class="k">return</span> <span class="n">row</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">setrowtrace</span><span class="p">(</span><span class="n">rowtrace</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select x,y from foo where x>3"</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| SQL: select x,y from foo where x>3
| Row: (4, 'five')
| Row: (7, 'eight')
| Row: ('one', 'two')
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Clear tracers</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">setrowtrace</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">setexectrace</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
<span class="c">###</span>
<span class="c">### executemany</span>
<span class="c">###</span>
<span class="c"># (This will work correctly with multiple statements, as well as statements that</span>
<span class="c"># return data. The second argument can be anything that is iterable.)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">"insert into foo (x) values(?)"</span><span class="p">,</span> <span class="p">(</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="p">)</span> <span class="p">)</span>
<span class="c"># You can also use it for statements that return data</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">"select * from foo where x=?"</span><span class="p">,</span> <span class="p">(</span> <span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">]</span> <span class="p">)</span> <span class="p">):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
<div class="highlight-python" id="scalar-example"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### defining your own functions</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">ilove7</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="s">"a scalar function"</span>
<span class="k">print</span> <span class="s">"ilove7 got"</span><span class="p">,</span><span class="n">args</span><span class="p">,</span><span class="s">"but I love 7"</span>
<span class="k">return</span> <span class="mi">7</span>
<span class="n">connection</span><span class="o">.</span><span class="n">createscalarfunction</span><span class="p">(</span><span class="s">"seven"</span><span class="p">,</span> <span class="n">ilove7</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select seven(x,y) from foo"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| ilove7 got (1, 2) but I love 7
| (7,)
| ilove7 got (4, 'five') but I love 7
| (7,)
| ilove7 got (7, 'eight') but I love 7
| (7,)
| ilove7 got ('one', 'two') but I love 7
| (7,)
| ilove7 got (1, 2) but I love 7
| (7,)
| ilove7 got (1, None) but I love 7
| (7,)
| ilove7 got (2, None) but I love 7
| (7,)
| ilove7 got (3, None) but I love 7
| (7,)
</pre></div>
</div>
<div class="highlight-python" id="aggregate-example"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### aggregate functions are more complex</span>
<span class="c">###</span>
<span class="c"># Here we return the longest item when represented as a string.</span>
<span class="k">class</span> <span class="nc">longest</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">longest</span><span class="o">=</span><span class="s">""</span>
<span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span> <span class="nb">str</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span> <span class="p">)</span> <span class="o">></span> <span class="nb">len</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">longest</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">longest</span><span class="o">=</span><span class="nb">str</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">final</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">longest</span>
<span class="c"># Under Python 2.3 remove the following line and add</span>
<span class="c"># factory=classmethod(factory) at the end</span>
<span class="nd">@classmethod</span>
<span class="k">def</span> <span class="nf">factory</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span>
<span class="k">return</span> <span class="n">cls</span><span class="p">(),</span> <span class="n">cls</span><span class="o">.</span><span class="n">step</span><span class="p">,</span> <span class="n">cls</span><span class="o">.</span><span class="n">final</span>
<span class="n">connection</span><span class="o">.</span><span class="n">createaggregatefunction</span><span class="p">(</span><span class="s">"longest"</span><span class="p">,</span> <span class="n">longest</span><span class="o">.</span><span class="n">factory</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select longest(x,y) from foo"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| ('eight',)
</pre></div>
</div>
<div class="highlight-python" id="collation-example"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Defining collations.</span>
<span class="c">###</span>
<span class="c"># The default sorting mechanisms don't understand numbers at the end of strings</span>
<span class="c"># so here we define a collation that does</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"create table s(str)"</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">"insert into s values(?)"</span><span class="p">,</span>
<span class="p">(</span> <span class="p">[</span><span class="s">"file1"</span><span class="p">],</span> <span class="p">[</span><span class="s">"file7"</span><span class="p">],</span> <span class="p">[</span><span class="s">"file17"</span><span class="p">],</span> <span class="p">[</span><span class="s">"file20"</span><span class="p">],</span> <span class="p">[</span><span class="s">"file3"</span><span class="p">]</span> <span class="p">)</span> <span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select * from s order by str"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| ('file1',)
| ('file17',)
| ('file20',)
| ('file3',)
| ('file7',)
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">strnumcollate</span><span class="p">(</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">):</span>
<span class="c"># return -1 if s1<s2, +1 if s1>s2 else 0</span>
<span class="c"># split values into two parts - the head and the numeric tail</span>
<span class="n">values</span><span class="o">=</span><span class="p">[</span><span class="n">s1</span><span class="p">,</span> <span class="n">s2</span><span class="p">]</span>
<span class="k">for</span> <span class="n">vn</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">values</span><span class="p">):</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">),</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">):</span>
<span class="k">if</span> <span class="n">v</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="s">"01234567890"</span><span class="p">:</span>
<span class="k">break</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">v</span><span class="o">=</span><span class="p">(</span> <span class="n">v</span><span class="p">[:</span><span class="n">i</span><span class="p">],</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">[</span><span class="n">i</span><span class="p">:])</span> <span class="p">)</span>
<span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
<span class="n">v</span><span class="o">=</span><span class="p">(</span> <span class="n">v</span><span class="p">[:</span><span class="n">i</span><span class="p">],</span> <span class="bp">None</span> <span class="p">)</span>
<span class="n">values</span><span class="p">[</span><span class="n">vn</span><span class="p">]</span><span class="o">=</span><span class="n">v</span>
<span class="c"># compare</span>
<span class="k">if</span> <span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o"><</span><span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span>
<span class="k">if</span> <span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">></span><span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">]:</span>
<span class="k">return</span> <span class="mi">1</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="n">connection</span><span class="o">.</span><span class="n">createcollation</span><span class="p">(</span><span class="s">"strnum"</span><span class="p">,</span> <span class="n">strnumcollate</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select * from s order by str collate strnum"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| ('file1',)
| ('file3',)
| ('file7',)
| ('file17',)
| ('file20',)
</pre></div>
</div>
<div class="highlight-python" id="authorizer-example"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Authorizer (eg if you want to control what user supplied SQL can do)</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">authorizer</span><span class="p">(</span><span class="n">operation</span><span class="p">,</span> <span class="n">paramone</span><span class="p">,</span> <span class="n">paramtwo</span><span class="p">,</span> <span class="n">databasename</span><span class="p">,</span> <span class="n">triggerorview</span><span class="p">):</span>
<span class="sd">"""Called when each operation is prepared. We can return SQLITE_OK, SQLITE_DENY or</span>
<span class="sd"> SQLITE_IGNORE"""</span>
<span class="c"># find the operation name</span>
<span class="k">print</span> <span class="n">apsw</span><span class="o">.</span><span class="n">mapping_authorizer_function</span><span class="p">[</span><span class="n">operation</span><span class="p">],</span>
<span class="k">print</span> <span class="n">paramone</span><span class="p">,</span> <span class="n">paramtwo</span><span class="p">,</span> <span class="n">databasename</span><span class="p">,</span> <span class="n">triggerorview</span>
<span class="k">if</span> <span class="n">operation</span><span class="o">==</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_CREATE_TABLE</span> <span class="ow">and</span> <span class="n">paramone</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">"private"</span><span class="p">):</span>
<span class="k">return</span> <span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_DENY</span> <span class="c"># not allowed to create tables whose names start with private</span>
<span class="k">return</span> <span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_OK</span> <span class="c"># always allow</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setauthorizer</span><span class="p">(</span><span class="n">authorizer</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into s values('foo')"</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select str from s limit 1"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| SQLITE_INSERT s None main None
| SQLITE_SELECT None None None None
| SQLITE_READ s str main None
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Cancel authorizer</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setauthorizer</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python" id="example-progress-handler"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### progress handler (SQLite 3 experimental feature)</span>
<span class="c">###</span>
<span class="c"># something to give us large numbers of random numbers</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="k">def</span> <span class="nf">randomintegers</span><span class="p">(</span><span class="n">howmany</span><span class="p">):</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">howmany</span><span class="p">):</span>
<span class="k">yield</span> <span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">9999999999</span><span class="p">),)</span>
<span class="c"># create a table with 100 random numbers</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"begin ; create table bigone(x)"</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">"insert into bigone values(?)"</span><span class="p">,</span> <span class="n">randomintegers</span><span class="p">(</span><span class="mi">100</span><span class="p">))</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"commit"</span><span class="p">)</span>
<span class="c"># display an ascii spinner</span>
<span class="n">_phcount</span><span class="o">=</span><span class="mi">0</span>
<span class="n">_phspinner</span><span class="o">=</span><span class="s">"|/-</span><span class="se">\\</span><span class="s">"</span>
<span class="k">def</span> <span class="nf">progresshandler</span><span class="p">():</span>
<span class="k">global</span> <span class="n">_phcount</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">_phspinner</span><span class="p">[</span><span class="n">_phcount</span><span class="o">%</span><span class="nb">len</span><span class="p">(</span><span class="n">_phspinner</span><span class="p">)]</span><span class="o">+</span><span class="nb">chr</span><span class="p">(</span><span class="mi">8</span><span class="p">))</span> <span class="c"># chr(8) is backspace</span>
<span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="n">_phcount</span><span class="o">+=</span><span class="mi">1</span>
<span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span> <span class="c"># deliberate delay so we can see the spinner (SQLite is too fast otherwise!)</span>
<span class="k">return</span> <span class="mi">0</span> <span class="c"># returning non-zero aborts</span>
<span class="c"># register progresshandler every 20 instructions</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setprogresshandler</span><span class="p">(</span><span class="n">progresshandler</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span>
<span class="c"># see it in action - sorting 100 numbers to find the biggest takes a while</span>
<span class="k">print</span> <span class="s">"spinny thing -> "</span><span class="p">,</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select max(x) from bigone"</span><span class="p">):</span>
<span class="k">print</span> <span class="c"># newline</span>
<span class="k">print</span> <span class="n">i</span> <span class="c"># and the maximum number</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setprogresshandler</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python" id="example-commithook"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### commit hook (SQLite3 experimental feature)</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">mycommithook</span><span class="p">():</span>
<span class="k">print</span> <span class="s">"in commit hook"</span>
<span class="n">hour</span><span class="o">=</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">()[</span><span class="mi">3</span><span class="p">]</span>
<span class="k">if</span> <span class="n">hour</span><span class="o"><</span><span class="mi">8</span> <span class="ow">or</span> <span class="n">hour</span><span class="o">></span><span class="mi">17</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"no commits out of hours"</span>
<span class="k">return</span> <span class="mi">1</span> <span class="c"># abort commits outside of 8am through 6pm</span>
<span class="k">print</span> <span class="s">"commits okay at this time"</span>
<span class="k">return</span> <span class="mi">0</span> <span class="c"># let commit go ahead</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setcommithook</span><span class="p">(</span><span class="n">mycommithook</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"begin; create table example(x,y,z); insert into example values (3,4,5) ; commit"</span><span class="p">)</span>
<span class="k">except</span> <span class="n">apsw</span><span class="o">.</span><span class="n">ConstraintError</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"commit was not allowed"</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setcommithook</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| in commit hook
| no commits out of hours
| commit was not allowed
</pre></div>
</div>
<div class="highlight-python" id="example-updatehook"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### update hook</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">myupdatehook</span><span class="p">(</span><span class="nb">type</span><span class="p">,</span> <span class="n">databasename</span><span class="p">,</span> <span class="n">tablename</span><span class="p">,</span> <span class="n">rowid</span><span class="p">):</span>
<span class="k">print</span> <span class="s">"Updated: </span><span class="si">%s</span><span class="s"> database </span><span class="si">%s</span><span class="s">, table </span><span class="si">%s</span><span class="s">, row </span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="p">(</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">mapping_authorizer_function</span><span class="p">[</span><span class="nb">type</span><span class="p">],</span> <span class="n">databasename</span><span class="p">,</span> <span class="n">tablename</span><span class="p">,</span> <span class="n">rowid</span><span class="p">)</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setupdatehook</span><span class="p">(</span><span class="n">myupdatehook</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into s values(?)"</span><span class="p">,</span> <span class="p">(</span><span class="s">"file93"</span><span class="p">,))</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"update s set str=? where str=?"</span><span class="p">,</span> <span class="p">(</span><span class="s">"file94"</span><span class="p">,</span> <span class="s">"file93"</span><span class="p">))</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"delete from s where str=?"</span><span class="p">,</span> <span class="p">(</span><span class="s">"file94"</span><span class="p">,))</span>
<span class="n">connection</span><span class="o">.</span><span class="n">setupdatehook</span><span class="p">(</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| Updated: SQLITE_INSERT database main, table s, row 7
| Updated: SQLITE_UPDATE database main, table s, row 7
| Updated: SQLITE_DELETE database main, table s, row 7
</pre></div>
</div>
<div class="highlight-python" id="example-blobio"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Blob I/O</span>
<span class="c">###</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"create table blobby(x,y)"</span><span class="p">)</span>
<span class="c"># Add a blob we will fill in later</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into blobby values(1,zeroblob(10000))"</span><span class="p">)</span>
<span class="c"># Or as a binding</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into blobby values(2,?)"</span><span class="p">,</span> <span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">zeroblob</span><span class="p">(</span><span class="mi">20000</span><span class="p">),))</span>
<span class="c"># Open a blob for writing. We need to know the rowid</span>
<span class="n">rowid</span><span class="o">=</span><span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select ROWID from blobby where x=1"</span><span class="p">)</span><span class="o">.</span><span class="n">next</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">blob</span><span class="o">=</span><span class="n">connection</span><span class="o">.</span><span class="n">blobopen</span><span class="p">(</span><span class="s">"main"</span><span class="p">,</span> <span class="s">"blobby"</span><span class="p">,</span> <span class="s">"y"</span><span class="p">,</span> <span class="n">rowid</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c"># 1 is for read/write</span>
<span class="n">blob</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"hello world"</span><span class="p">)</span>
<span class="n">blob</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">2000</span><span class="p">)</span>
<span class="n">blob</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"hello world, again"</span><span class="p">)</span>
<span class="n">blob</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-python" id="example-vtable"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Virtual tables</span>
<span class="c">###</span>
<span class="c"># This virtual table stores information about files in a set of</span>
<span class="c"># directories so you can execute SQL queries</span>
<span class="k">def</span> <span class="nf">getfiledata</span><span class="p">(</span><span class="n">directories</span><span class="p">):</span>
<span class="n">columns</span><span class="o">=</span><span class="bp">None</span>
<span class="n">data</span><span class="o">=</span><span class="p">[]</span>
<span class="n">counter</span><span class="o">=</span><span class="mi">1</span>
<span class="k">for</span> <span class="n">directory</span> <span class="ow">in</span> <span class="n">directories</span><span class="p">:</span>
<span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">directory</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isfile</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span><span class="n">f</span><span class="p">)):</span>
<span class="k">continue</span>
<span class="n">counter</span><span class="o">+=</span><span class="mi">1</span>
<span class="n">st</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">stat</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">directory</span><span class="p">,</span><span class="n">f</span><span class="p">))</span>
<span class="k">if</span> <span class="n">columns</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
<span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s">"rowid"</span><span class="p">,</span> <span class="s">"name"</span><span class="p">,</span> <span class="s">"directory"</span><span class="p">]</span><span class="o">+</span><span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">st</span><span class="p">)</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">"st_"</span><span class="p">)]</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span> <span class="p">[</span><span class="n">counter</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">directory</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="nb">getattr</span><span class="p">(</span><span class="n">st</span><span class="p">,</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">[</span><span class="mi">3</span><span class="p">:]]</span> <span class="p">)</span>
<span class="k">return</span> <span class="n">columns</span><span class="p">,</span> <span class="n">data</span>
<span class="c"># This gets registered with the Connection</span>
<span class="k">class</span> <span class="nc">Source</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">Create</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db</span><span class="p">,</span> <span class="n">modulename</span><span class="p">,</span> <span class="n">dbname</span><span class="p">,</span> <span class="n">tablename</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="n">columns</span><span class="p">,</span><span class="n">data</span><span class="o">=</span><span class="n">getfiledata</span><span class="p">([</span><span class="nb">eval</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">"</span><span class="se">\\</span><span class="s">"</span><span class="p">,</span> <span class="s">"</span><span class="se">\\\\</span><span class="s">"</span><span class="p">))</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">])</span> <span class="c"># eval strips off layer of quotes</span>
<span class="n">schema</span><span class="o">=</span><span class="s">"create table foo("</span><span class="o">+</span><span class="s">','</span><span class="o">.</span><span class="n">join</span><span class="p">([</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">x</span><span class="p">,)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">columns</span><span class="p">[</span><span class="mi">1</span><span class="p">:]])</span><span class="o">+</span><span class="s">")"</span>
<span class="k">return</span> <span class="n">schema</span><span class="p">,</span><span class="n">Table</span><span class="p">(</span><span class="n">columns</span><span class="p">,</span><span class="n">data</span><span class="p">)</span>
<span class="n">Connect</span><span class="o">=</span><span class="n">Create</span>
<span class="c"># Represents a table</span>
<span class="k">class</span> <span class="nc">Table</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">columns</span><span class="p">,</span> <span class="n">data</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">columns</span><span class="o">=</span><span class="n">columns</span>
<span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">=</span><span class="n">data</span>
<span class="k">def</span> <span class="nf">BestIndex</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">None</span>
<span class="k">def</span> <span class="nf">Open</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="n">Cursor</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">Disconnect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="n">Destroy</span><span class="o">=</span><span class="n">Disconnect</span>
<span class="c"># Represents a cursor</span>
<span class="k">class</span> <span class="nc">Cursor</span><span class="p">:</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">table</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">=</span><span class="n">table</span>
<span class="k">def</span> <span class="nf">Filter</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="o">=</span><span class="mi">0</span>
<span class="k">def</span> <span class="nf">Eof</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="o">>=</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">data</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">Rowid</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">Column</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">col</span><span class="p">):</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">table</span><span class="o">.</span><span class="n">data</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="p">][</span><span class="mi">1</span><span class="o">+</span><span class="n">col</span><span class="p">]</span>
<span class="k">def</span> <span class="nf">Next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pos</span><span class="o">+=</span><span class="mi">1</span>
<span class="k">def</span> <span class="nf">Close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="c"># Register the module as filesource</span>
<span class="n">connection</span><span class="o">.</span><span class="n">createmodule</span><span class="p">(</span><span class="s">"filesource"</span><span class="p">,</span> <span class="n">Source</span><span class="p">())</span>
<span class="c"># Arguments to module - all directories in sys.path</span>
<span class="n">sysdirs</span><span class="o">=</span><span class="s">","</span><span class="o">.</span><span class="n">join</span><span class="p">([</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">x</span><span class="p">,)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">path</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="ow">and</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">x</span><span class="p">)])</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"create virtual table sysfiles using filesource("</span><span class="o">+</span><span class="n">sysdirs</span><span class="o">+</span><span class="s">")"</span><span class="p">)</span>
<span class="c"># Which 3 files are the biggest?</span>
<span class="k">for</span> <span class="n">size</span><span class="p">,</span><span class="n">directory</span><span class="p">,</span><span class="nb">file</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select st_size,directory,name from sysfiles order by st_size desc limit 3"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">size</span><span class="p">,</span><span class="nb">file</span><span class="p">,</span><span class="n">directory</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| 9510280 qt.so /usr/lib/pymodules/python2.7
| 3226728 apsw.o /space/apsw
| 3133370 apsw.so /space/apsw
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Which 3 files are the oldest?</span>
<span class="k">for</span> <span class="n">ctime</span><span class="p">,</span><span class="n">directory</span><span class="p">,</span><span class="nb">file</span> <span class="ow">in</span> <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"select st_ctime,directory,name from sysfiles order by st_ctime limit 3"</span><span class="p">):</span>
<span class="k">print</span> <span class="n">ctime</span><span class="p">,</span><span class="nb">file</span><span class="p">,</span><span class="n">directory</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| 1288928270.12 LICENSE /space/apsw
| 1288928270.6 setup.cfg /space/apsw
| 1289174179.62 pexpect-2.3.egg-info /usr/lib/python2.7/dist-packages
</pre></div>
</div>
<div class="highlight-python" id="example-vfs"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### A VFS that "obfuscates" the database file contents. The scheme</span>
<span class="c">### used is to xor all bytes with 0xa5. This scheme honours that used</span>
<span class="c">### for MAPI and SQL Server.</span>
<span class="c">###</span>
<span class="k">def</span> <span class="nf">encryptme</span><span class="p">(</span><span class="n">data</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">data</span><span class="p">:</span> <span class="k">return</span> <span class="n">data</span>
<span class="k">return</span> <span class="s">""</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">chr</span><span class="p">(</span><span class="nb">ord</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">^</span><span class="mh">0xa5</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">data</span><span class="p">])</span>
<span class="c"># Inheriting from a base of "" means the default vfs</span>
<span class="k">class</span> <span class="nc">ObfuscatedVFS</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">VFS</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">vfsname</span><span class="o">=</span><span class="s">"obfu"</span><span class="p">,</span> <span class="n">basevfs</span><span class="o">=</span><span class="s">""</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">vfsname</span><span class="o">=</span><span class="n">vfsname</span>
<span class="bp">self</span><span class="o">.</span><span class="n">basevfs</span><span class="o">=</span><span class="n">basevfs</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">VFS</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">vfsname</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">basevfs</span><span class="p">)</span>
<span class="c"># We want to return our own file implmentation, but also</span>
<span class="c"># want it to inherit</span>
<span class="k">def</span> <span class="nf">xOpen</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">flags</span><span class="p">):</span>
<span class="k">return</span> <span class="n">ObfuscatedVFSFile</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">basevfs</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span>
<span class="c"># The file implementation where we override xRead and xWrite to call our</span>
<span class="c"># encryption routine</span>
<span class="k">class</span> <span class="nc">ObfuscatedVFSFile</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">VFSFile</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inheritfromvfsname</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">flags</span><span class="p">):</span>
<span class="n">apsw</span><span class="o">.</span><span class="n">VFSFile</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">inheritfromvfsname</span><span class="p">,</span> <span class="n">filename</span><span class="p">,</span> <span class="n">flags</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">xRead</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">amount</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
<span class="k">return</span> <span class="n">encryptme</span><span class="p">(</span><span class="nb">super</span><span class="p">(</span><span class="n">ObfuscatedVFSFile</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">xRead</span><span class="p">(</span><span class="n">amount</span><span class="p">,</span> <span class="n">offset</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">xWrite</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">offset</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">ObfuscatedVFSFile</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">xWrite</span><span class="p">(</span><span class="n">encryptme</span><span class="p">(</span><span class="n">data</span><span class="p">),</span> <span class="n">offset</span><span class="p">)</span>
<span class="c"># To register the VFS we just instantiate it</span>
<span class="n">obfuvfs</span><span class="o">=</span><span class="n">ObfuscatedVFS</span><span class="p">()</span>
<span class="c"># Lets see what vfs are now available?</span>
<span class="k">print</span> <span class="n">apsw</span><span class="o">.</span><span class="n">vfsnames</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| ['unix', 'obfu', 'unix-excl', 'unix-dotfile', 'unix-none']
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Make an obfuscated db</span>
<span class="n">obfudb</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="s">"myobfudb"</span><span class="p">,</span> <span class="n">vfs</span><span class="o">=</span><span class="n">obfuvfs</span><span class="o">.</span><span class="n">vfsname</span><span class="p">)</span>
<span class="c"># Check it works</span>
<span class="n">obfudb</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">"create table foo(x,y); insert into foo values(1,2)"</span><span class="p">)</span>
<span class="c"># Check it really is obfuscated on disk</span>
<span class="k">print</span> <span class="sb">`open("myobfudb", "rb").read()[:20]`</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| '\xf6\xf4\xe9\xcc\xd1\xc0\x85\xc3\xca\xd7\xc8\xc4\xd1\x85\x96\xa5\xa1\xa5\xa4\xa4'
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># And unobfuscating it</span>
<span class="k">print</span> <span class="sb">`encryptme(open("myobfudb", "rb").read()[:20])`</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| 'SQLite format 3\x00\x04\x00\x01\x01'
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Tidy up</span>
<span class="n">obfudb</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">"myobfudb"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python" id="example-limit"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Limits</span>
<span class="c">###</span>
<span class="c"># Print some limits</span>
<span class="k">for</span> <span class="n">limit</span> <span class="ow">in</span> <span class="p">(</span><span class="s">"LENGTH"</span><span class="p">,</span> <span class="s">"COLUMN"</span><span class="p">,</span> <span class="s">"ATTACHED"</span><span class="p">):</span>
<span class="n">name</span><span class="o">=</span><span class="s">"SQLITE_LIMIT_"</span><span class="o">+</span><span class="n">limit</span>
<span class="n">maxname</span><span class="o">=</span><span class="s">"SQLITE_MAX_"</span><span class="o">+</span><span class="n">limit</span> <span class="c"># compile time</span>
<span class="n">orig</span><span class="o">=</span><span class="n">connection</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">apsw</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
<span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">orig</span>
<span class="c"># To get the maximum, set to 0x7fffffff and then read value back</span>
<span class="n">connection</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">apsw</span><span class="p">,</span> <span class="n">name</span><span class="p">),</span> <span class="mh">0x7fffffff</span><span class="p">)</span>
<span class="nb">max</span><span class="o">=</span><span class="n">connection</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">apsw</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>
<span class="k">print</span> <span class="n">maxname</span><span class="p">,</span> <span class="nb">max</span>
<span class="c"># Set limit for size of a string</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"create table testlimit(s)"</span><span class="p">)</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into testlimit values(?)"</span><span class="p">,</span> <span class="p">(</span> <span class="s">"x"</span><span class="o">*</span><span class="mi">1024</span><span class="p">,</span> <span class="p">))</span> <span class="c"># 1024 char string</span>
<span class="n">connection</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_LIMIT_LENGTH</span><span class="p">,</span> <span class="mi">1023</span><span class="p">)</span> <span class="c"># limit is now 1023</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">"insert into testlimit values(?)"</span><span class="p">,</span> <span class="p">(</span> <span class="s">"y"</span><span class="o">*</span><span class="mi">1024</span><span class="p">,</span> <span class="p">))</span>
<span class="k">print</span> <span class="s">"string exceeding limit was inserted"</span>
<span class="k">except</span> <span class="n">apsw</span><span class="o">.</span><span class="n">TooBigError</span><span class="p">:</span>
<span class="k">print</span> <span class="s">"Caught toobig exception"</span>
<span class="n">connection</span><span class="o">.</span><span class="n">limit</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_LIMIT_LENGTH</span><span class="p">,</span> <span class="mh">0x7fffffff</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| SQLITE_LIMIT_LENGTH 1000000000
| SQLITE_MAX_LENGTH 1000000000
| SQLITE_LIMIT_COLUMN 2000
| SQLITE_MAX_COLUMN 2000
| SQLITE_LIMIT_ATTACHED 10
| SQLITE_MAX_ATTACHED 10
| Caught toobig exception
</pre></div>
</div>
<div class="highlight-python" id="example-backup"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Backup to memory</span>
<span class="c">###</span>
<span class="c"># We will copy the disk database into a memory database</span>
<span class="n">memcon</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="s">":memory:"</span><span class="p">)</span>
<span class="c"># Copy into memory</span>
<span class="k">with</span> <span class="n">memcon</span><span class="o">.</span><span class="n">backup</span><span class="p">(</span><span class="s">"main"</span><span class="p">,</span> <span class="n">connection</span><span class="p">,</span> <span class="s">"main"</span><span class="p">)</span> <span class="k">as</span> <span class="n">backup</span><span class="p">:</span>
<span class="n">backup</span><span class="o">.</span><span class="n">step</span><span class="p">()</span> <span class="c"># copy whole database in one go</span>
<span class="c"># There will be no disk accesses for this query</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">memcon</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">"select * from s"</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<div class="highlight-python" id="example-shell"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Shell</span>
<span class="c">###</span>
<span class="c"># Here we use the shell to do a csv export providing the existing db</span>
<span class="c"># connection</span>
<span class="c"># Export to a StringIO</span>
<span class="kn">import</span> <span class="nn">StringIO</span> <span class="kn">as</span> <span class="nn">io</span> <span class="c"># use io in Python 3</span>
<span class="n">output</span><span class="o">=</span><span class="n">io</span><span class="o">.</span><span class="n">StringIO</span><span class="p">()</span>
<span class="n">shell</span><span class="o">=</span><span class="n">apsw</span><span class="o">.</span><span class="n">Shell</span><span class="p">(</span><span class="n">stdout</span><span class="o">=</span><span class="n">output</span><span class="p">,</span> <span class="n">db</span><span class="o">=</span><span class="n">connection</span><span class="p">)</span>
<span class="c"># How to execute a dot command</span>
<span class="n">shell</span><span class="o">.</span><span class="n">process_command</span><span class="p">(</span><span class="s">".mode csv"</span><span class="p">)</span>
<span class="c"># How to execute SQL</span>
<span class="n">shell</span><span class="o">.</span><span class="n">process_sql</span><span class="p">(</span><span class="s">"create table csvtest(x,y); insert into csvtest values(3,4); insert into csvtest values('ab', NULL)"</span><span class="p">)</span>
<span class="c"># Let the shell figure out SQL vs dot command</span>
<span class="n">shell</span><span class="o">.</span><span class="n">process_complete_line</span><span class="p">(</span><span class="s">"select * from csvtest"</span><span class="p">)</span>
<span class="c"># Verify output</span>
<span class="k">print</span> <span class="n">output</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| 3,4
| ab,
|
</pre></div>
</div>
<div class="highlight-python" id="example-status"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Statistics</span>
<span class="c">###</span>
<span class="k">print</span> <span class="s">"SQLite memory usage current </span><span class="si">%d</span><span class="s"> max </span><span class="si">%d</span><span class="s">"</span> <span class="o">%</span> <span class="n">apsw</span><span class="o">.</span><span class="n">status</span><span class="p">(</span><span class="n">apsw</span><span class="o">.</span><span class="n">SQLITE_STATUS_MEMORY_USED</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-text"><div class="highlight"><pre>| SQLite memory usage current 313784 max 318624
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="c">###</span>
<span class="c">### Cleanup</span>
<span class="c">###</span>
<span class="c"># We can close connections manually (useful if you want to catch exceptions)</span>
<span class="c"># but you don't have to</span>
<span class="n">connection</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="c"># force it since we want to exit</span>
<span class="c"># Delete database - we don't need it any more</span>
<span class="n">os</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="s">"dbfile"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h4>Previous topic</h4>
<p class="topless"><a href="tips.html"
title="previous chapter">Tips</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="download.html"
title="next chapter">Download</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/example.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" />
<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">
<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="download.html" title="Download"
>next</a> |</li>
<li class="right" >
<a href="tips.html" title="Tips"
>previous</a> |</li>
<li><a href="index.html">APSW 3.7.6.3-r1 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© <a href="copyright.html">Copyright</a> 2004-2010, Roger Binns <rogerb@rogerbinns.com>.
Last updated on May 20, 2011.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1pre/4b8d012cf82e.
</div>
</body>
</html>
|