This file is indexed.

/usr/share/doc/python-pysqlite2-doc/html/sqlite3.html is in python-pysqlite2-doc 2.6.3-3.

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
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
<!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>sqlite3 — DB-API 2.0 interface for SQLite databases &mdash; pysqlite 2.6.0 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:     '2.6.0',
        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="top" title="pysqlite 2.6.0 documentation" href="index.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><a href="index.html">pysqlite 2.6.0 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-sqlite3">
<span id="sqlite3-db-api-2-0-interface-for-sqlite-databases"></span><h1><a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> &#8212; DB-API 2.0 interface for SQLite databases<a class="headerlink" href="#module-sqlite3" title="Permalink to this headline"></a></h1>
<p>SQLite is a C library that provides a lightweight disk-based database that
doesn&#8217;t require a separate server process and allows accessing the database
using a nonstandard variant of the SQL query language. Some applications can use
SQLite for internal data storage.  It&#8217;s also possible to prototype an
application using SQLite and then port the code to a larger database such as
PostgreSQL or Oracle.</p>
<p>pysqlite was written by Gerhard Häring and provides a SQL interface compliant
with the DB-API 2.0 specification described by <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0249"><strong>PEP 249</strong></a>.</p>
<p>To use the module, you must first create a <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object that
represents the database.  Here the data will be stored in the
<tt class="file docutils literal"><span class="pre">/tmp/example</span></tt> file:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">conn</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&#39;/tmp/example&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>You can also supply the special name <tt class="docutils literal"><span class="pre">:memory:</span></tt> to create a database in RAM.</p>
<p>Once you have a <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a>, you can create a <tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt>  object
and call its <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><tt class="xref py py-meth docutils literal"><span class="pre">execute()</span></tt></a> method to perform SQL commands:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="c"># Create table</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;&#39;&#39;create table stocks</span>
<span class="s">(date text, trans text, symbol text,</span>
<span class="s"> qty real, price real)&#39;&#39;&#39;</span><span class="p">)</span>

<span class="c"># Insert a row of data</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;&quot;&quot;insert into stocks</span>
<span class="s">          values (&#39;2006-01-05&#39;,&#39;BUY&#39;,&#39;RHAT&#39;,100,35.14)&quot;&quot;&quot;</span><span class="p">)</span>

<span class="c"># Save (commit) the changes</span>
<span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

<span class="c"># We can also close the cursor if we are done with it</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Usually your SQL operations will need to use values from Python variables.  You
shouldn&#8217;t assemble your query using Python&#8217;s string operations because doing so
is insecure; it makes your program vulnerable to an SQL injection attack.</p>
<p>Instead, use the DB-API&#8217;s parameter substitution.  Put <tt class="docutils literal"><span class="pre">?</span></tt> as a placeholder
wherever you want to use a value, and then provide a tuple of values as the
second argument to the cursor&#8217;s <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><tt class="xref py py-meth docutils literal"><span class="pre">execute()</span></tt></a> method.  (Other database
modules may use a different placeholder, such as <tt class="docutils literal"><span class="pre">%s</span></tt> or <tt class="docutils literal"><span class="pre">:1</span></tt>.) For
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Never do this -- insecure!</span>
<span class="n">symbol</span> <span class="o">=</span> <span class="s">&#39;IBM&#39;</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;... where symbol = &#39;</span><span class="si">%s</span><span class="s">&#39;&quot;</span> <span class="o">%</span> <span class="n">symbol</span><span class="p">)</span>

<span class="c"># Do this instead</span>
<span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="n">symbol</span><span class="p">,)</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;select * from stocks where symbol=?&#39;</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>

<span class="c"># Larger example</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="p">[(</span><span class="s">&#39;2006-03-28&#39;</span><span class="p">,</span> <span class="s">&#39;BUY&#39;</span><span class="p">,</span> <span class="s">&#39;IBM&#39;</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mf">45.00</span><span class="p">),</span>
          <span class="p">(</span><span class="s">&#39;2006-04-05&#39;</span><span class="p">,</span> <span class="s">&#39;BUY&#39;</span><span class="p">,</span> <span class="s">&#39;MSOFT&#39;</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mf">72.00</span><span class="p">),</span>
          <span class="p">(</span><span class="s">&#39;2006-04-06&#39;</span><span class="p">,</span> <span class="s">&#39;SELL&#39;</span><span class="p">,</span> <span class="s">&#39;IBM&#39;</span><span class="p">,</span> <span class="mi">500</span><span class="p">,</span> <span class="mf">53.00</span><span class="p">),</span>
         <span class="p">]:</span>
    <span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;insert into stocks values (?,?,?,?,?)&#39;</span><span class="p">,</span> <span class="n">t</span><span class="p">)</span>
</pre></div>
</div>
<p>To retrieve data after executing a SELECT statement, you can either treat the
cursor as an <em class="xref std std-term">iterator</em>, call the cursor&#8217;s <a class="reference internal" href="#sqlite3.Cursor.fetchone" title="sqlite3.Cursor.fetchone"><tt class="xref py py-meth docutils literal"><span class="pre">fetchone()</span></tt></a> method to
retrieve a single matching row, or call <a class="reference internal" href="#sqlite3.Cursor.fetchall" title="sqlite3.Cursor.fetchall"><tt class="xref py py-meth docutils literal"><span class="pre">fetchall()</span></tt></a> to get a list of the
matching rows.</p>
<p>This example uses the iterator form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;select * from stocks order by price&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
<span class="gp">... </span>   <span class="k">print</span> <span class="n">row</span>
<span class="gp">...</span>
<span class="go">(u&#39;2006-01-05&#39;, u&#39;BUY&#39;, u&#39;RHAT&#39;, 100, 35.14)</span>
<span class="go">(u&#39;2006-03-28&#39;, u&#39;BUY&#39;, u&#39;IBM&#39;, 1000, 45.0)</span>
<span class="go">(u&#39;2006-04-06&#39;, u&#39;SELL&#39;, u&#39;IBM&#39;, 500, 53.0)</span>
<span class="go">(u&#39;2006-04-05&#39;, u&#39;BUY&#39;, u&#39;MSOFT&#39;, 1000, 72.0)</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://code.google.com/p/pysqlite/">http://code.google.com/p/pysqlite/</a></dt>
<dd>The pysqlite web page &#8211; sqlite3 is developed externally under the name
&#8220;pysqlite&#8221;.</dd>
<dt><a class="reference external" href="http://www.sqlite.org">http://www.sqlite.org</a></dt>
<dd>The SQLite web page; the documentation describes the syntax and the
available data types for the supported SQL dialect.</dd>
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0249"><strong>PEP 249</strong></a> - Database API Specification 2.0</dt>
<dd>PEP written by Marc-André Lemburg.</dd>
</dl>
</div>
<div class="section" id="module-functions-and-constants">
<span id="sqlite3-module-contents"></span><h2>Module functions and constants<a class="headerlink" href="#module-functions-and-constants" title="Permalink to this headline"></a></h2>
<dl class="data">
<dt id="sqlite3.PARSE_DECLTYPES">
<tt class="descclassname">sqlite3.</tt><tt class="descname">PARSE_DECLTYPES</tt><a class="headerlink" href="#sqlite3.PARSE_DECLTYPES" title="Permalink to this definition"></a></dt>
<dd><p>This constant is meant to be used with the <em>detect_types</em> parameter of the
<a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a> function.</p>
<p>Setting it makes the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module parse the declared type for each
column it returns.  It will parse out the first word of the declared type,
i. e.  for &#8220;integer primary key&#8221;, it will parse out &#8220;integer&#8221;, or for
&#8220;number(10)&#8221; it will parse out &#8220;number&#8221;. Then for that column, it will look
into the converters dictionary and use the converter function registered for
that type there.</p>
</dd></dl>

<dl class="data">
<dt id="sqlite3.PARSE_COLNAMES">
<tt class="descclassname">sqlite3.</tt><tt class="descname">PARSE_COLNAMES</tt><a class="headerlink" href="#sqlite3.PARSE_COLNAMES" title="Permalink to this definition"></a></dt>
<dd><p>This constant is meant to be used with the <em>detect_types</em> parameter of the
<a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a> function.</p>
<p>Setting this makes the SQLite interface parse the column name for each column it
returns.  It will look for a string formed [mytype] in there, and then decide
that &#8216;mytype&#8217; is the type of the column. It will try to find an entry of
&#8216;mytype&#8217; in the converters dictionary and then use the converter function found
there to return the value. The column name found in <a class="reference internal" href="#sqlite3.Cursor.description" title="sqlite3.Cursor.description"><tt class="xref py py-attr docutils literal"><span class="pre">Cursor.description</span></tt></a>
is only the first word of the column name, i.  e. if you use something like
<tt class="docutils literal"><span class="pre">'as</span> <span class="pre">&quot;x</span> <span class="pre">[datetime]&quot;'</span></tt> in your SQL, then we will parse out everything until the
first blank for the column name: the column name would simply be &#8220;x&#8221;.</p>
</dd></dl>

<dl class="function">
<dt id="sqlite3.connect">
<tt class="descclassname">sqlite3.</tt><tt class="descname">connect</tt><big>(</big><em>database</em><span class="optional">[</span>, <em>timeout</em>, <em>isolation_level</em>, <em>detect_types</em>, <em>factory</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.connect" title="Permalink to this definition"></a></dt>
<dd><p>Opens a connection to the SQLite database file <em>database</em>. You can use
<tt class="docutils literal"><span class="pre">&quot;:memory:&quot;</span></tt> to open a database connection to a database that resides in RAM
instead of on disk.</p>
<p>When a database is accessed by multiple connections, and one of the processes
modifies the database, the SQLite database is locked until that transaction is
committed. The <em>timeout</em> parameter specifies how long the connection should wait
for the lock to go away until raising an exception. The default for the timeout
parameter is 5.0 (five seconds).</p>
<p>For the <em>isolation_level</em> parameter, please see the
<a class="reference internal" href="#sqlite3.Connection.isolation_level" title="sqlite3.Connection.isolation_level"><tt class="xref py py-attr docutils literal"><span class="pre">Connection.isolation_level</span></tt></a> property of <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> objects.</p>
<p>SQLite natively supports only the types TEXT, INTEGER, FLOAT, BLOB and NULL. If
you want to use other types you must add support for them yourself. The
<em>detect_types</em> parameter and the using custom <strong>converters</strong> registered with the
module-level <a class="reference internal" href="#sqlite3.register_converter" title="sqlite3.register_converter"><tt class="xref py py-func docutils literal"><span class="pre">register_converter()</span></tt></a> function allow you to easily do that.</p>
<p><em>detect_types</em> defaults to 0 (i. e. off, no type detection), you can set it to
any combination of <a class="reference internal" href="#sqlite3.PARSE_DECLTYPES" title="sqlite3.PARSE_DECLTYPES"><tt class="xref py py-const docutils literal"><span class="pre">PARSE_DECLTYPES</span></tt></a> and <a class="reference internal" href="#sqlite3.PARSE_COLNAMES" title="sqlite3.PARSE_COLNAMES"><tt class="xref py py-const docutils literal"><span class="pre">PARSE_COLNAMES</span></tt></a> to turn
type detection on.</p>
<p>By default, the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module uses its <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> class for the
connect call.  You can, however, subclass the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> class and make
<a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a> use your class instead by providing your class for the <em>factory</em>
parameter.</p>
<p>Consult the section <a class="reference internal" href="#sqlite3-types"><em>SQLite and Python types</em></a> of this manual for details.</p>
<p>The <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module internally uses a statement cache to avoid SQL parsing
overhead. If you want to explicitly set the number of statements that are cached
for the connection, you can set the <em>cached_statements</em> parameter. The currently
implemented default is to cache 100 statements.</p>
</dd></dl>

<dl class="function">
<dt id="sqlite3.register_converter">
<tt class="descclassname">sqlite3.</tt><tt class="descname">register_converter</tt><big>(</big><em>typename</em>, <em>callable</em><big>)</big><a class="headerlink" href="#sqlite3.register_converter" title="Permalink to this definition"></a></dt>
<dd><p>Registers a callable to convert a bytestring from the database into a custom
Python type. The callable will be invoked for all database values that are of
the type <em>typename</em>. Confer the parameter <em>detect_types</em> of the <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a>
function for how the type detection works. Note that the case of <em>typename</em> and
the name of the type in your query must match!</p>
</dd></dl>

<dl class="function">
<dt id="sqlite3.register_adapter">
<tt class="descclassname">sqlite3.</tt><tt class="descname">register_adapter</tt><big>(</big><em>type</em>, <em>callable</em><big>)</big><a class="headerlink" href="#sqlite3.register_adapter" title="Permalink to this definition"></a></dt>
<dd><p>Registers a callable to convert the custom Python type <em>type</em> into one of
SQLite&#8217;s supported types. The callable <em>callable</em> accepts as single parameter
the Python value, and must return a value of the following types: int, long,
float, str (UTF-8 encoded), unicode or buffer.</p>
</dd></dl>

<dl class="function">
<dt id="sqlite3.complete_statement">
<tt class="descclassname">sqlite3.</tt><tt class="descname">complete_statement</tt><big>(</big><em>sql</em><big>)</big><a class="headerlink" href="#sqlite3.complete_statement" title="Permalink to this definition"></a></dt>
<dd><p>Returns <tt class="xref py py-const docutils literal"><span class="pre">True</span></tt> if the string <em>sql</em> contains one or more complete SQL
statements terminated by semicolons. It does not verify that the SQL is
syntactically correct, only that there are no unclosed string literals and the
statement is terminated by a semicolon.</p>
<p>This can be used to build a shell for SQLite, as in the following example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># A minimal SQLite shell for experiments</span>

<span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">isolation_level</span> <span class="o">=</span> <span class="bp">None</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="nb">buffer</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>

<span class="k">print</span> <span class="s">&quot;Enter your SQL commands to execute in SQLite.&quot;</span>
<span class="k">print</span> <span class="s">&quot;Enter a blank line to exit.&quot;</span>

<span class="k">while</span> <span class="bp">True</span><span class="p">:</span>
    <span class="n">line</span> <span class="o">=</span> <span class="nb">raw_input</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="s">&quot;&quot;</span><span class="p">:</span>
        <span class="k">break</span>
    <span class="nb">buffer</span> <span class="o">+=</span> <span class="n">line</span>
    <span class="k">if</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">complete_statement</span><span class="p">(</span><span class="nb">buffer</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="nb">buffer</span> <span class="o">=</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="nb">buffer</span><span class="p">)</span>

            <span class="k">if</span> <span class="nb">buffer</span><span class="o">.</span><span class="n">lstrip</span><span class="p">()</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s">&quot;SELECT&quot;</span><span class="p">):</span>
                <span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
        <span class="k">except</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Error</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;An error occurred:&quot;</span><span class="p">,</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="nb">buffer</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>

<span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="sqlite3.enable_callback_tracebacks">
<tt class="descclassname">sqlite3.</tt><tt class="descname">enable_callback_tracebacks</tt><big>(</big><em>flag</em><big>)</big><a class="headerlink" href="#sqlite3.enable_callback_tracebacks" title="Permalink to this definition"></a></dt>
<dd><p>By default you will not get any tracebacks in user-defined functions,
aggregates, converters, authorizer callbacks etc. If you want to debug them, you
can call this function with <em>flag</em> as True. Afterwards, you will get tracebacks
from callbacks on <tt class="docutils literal"><span class="pre">sys.stderr</span></tt>. Use <tt class="xref py py-const docutils literal"><span class="pre">False</span></tt> to disable the feature
again.</p>
</dd></dl>

</div>
<div class="section" id="connection-objects">
<span id="sqlite3-connection-objects"></span><h2>Connection Objects<a class="headerlink" href="#connection-objects" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="sqlite3.Connection">
<em class="property">class </em><tt class="descclassname">sqlite3.</tt><tt class="descname">Connection</tt><a class="headerlink" href="#sqlite3.Connection" title="Permalink to this definition"></a></dt>
<dd><p>A SQLite database connection has the following attributes and methods:</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Connection.isolation_level">
<tt class="descclassname">Connection.</tt><tt class="descname">isolation_level</tt><a class="headerlink" href="#sqlite3.Connection.isolation_level" title="Permalink to this definition"></a></dt>
<dd><p>Get or set the current isolation level. <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt> for autocommit mode or
one of &#8220;DEFERRED&#8221;, &#8220;IMMEDIATE&#8221; or &#8220;EXCLUSIVE&#8221;. See section
<a class="reference internal" href="#sqlite3-controlling-transactions"><em>Controlling Transactions</em></a> for a more detailed explanation.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.cursor">
<tt class="descclassname">Connection.</tt><tt class="descname">cursor</tt><big>(</big><span class="optional">[</span><em>cursorClass</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.Connection.cursor" title="Permalink to this definition"></a></dt>
<dd><p>The cursor method accepts a single optional parameter <em>cursorClass</em>. If
supplied, this must be a custom cursor class that extends
<tt class="xref py py-class docutils literal"><span class="pre">sqlite3.Cursor</span></tt>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.commit">
<tt class="descclassname">Connection.</tt><tt class="descname">commit</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Connection.commit" title="Permalink to this definition"></a></dt>
<dd><p>This method commits the current transaction. If you don&#8217;t call this method,
anything you did since the last call to <tt class="docutils literal"><span class="pre">commit()</span></tt> is not visible from from
other database connections. If you wonder why you don&#8217;t see the data you&#8217;ve
written to the database, please check you didn&#8217;t forget to call this method.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.rollback">
<tt class="descclassname">Connection.</tt><tt class="descname">rollback</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Connection.rollback" title="Permalink to this definition"></a></dt>
<dd><p>This method rolls back any changes to the database since the last call to
<a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><tt class="xref py py-meth docutils literal"><span class="pre">commit()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.close">
<tt class="descclassname">Connection.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Connection.close" title="Permalink to this definition"></a></dt>
<dd><p>This closes the database connection. Note that this does not automatically
call <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><tt class="xref py py-meth docutils literal"><span class="pre">commit()</span></tt></a>. If you just close your database connection without
calling <a class="reference internal" href="#sqlite3.Connection.commit" title="sqlite3.Connection.commit"><tt class="xref py py-meth docutils literal"><span class="pre">commit()</span></tt></a> first, your changes will be lost!</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.execute">
<tt class="descclassname">Connection.</tt><tt class="descname">execute</tt><big>(</big><em>sql</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.Connection.execute" title="Permalink to this definition"></a></dt>
<dd><p>This is a nonstandard shortcut that creates an intermediate cursor object by
calling the cursor method, then calls the cursor&#8217;s
<a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><tt class="xref py py-meth docutils literal"><span class="pre">execute</span></tt></a> method with the parameters given.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.executemany">
<tt class="descclassname">Connection.</tt><tt class="descname">executemany</tt><big>(</big><em>sql</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.Connection.executemany" title="Permalink to this definition"></a></dt>
<dd><p>This is a nonstandard shortcut that creates an intermediate cursor object by
calling the cursor method, then calls the cursor&#8217;s
<a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><tt class="xref py py-meth docutils literal"><span class="pre">executemany</span></tt></a> method with the parameters given.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.executescript">
<tt class="descclassname">Connection.</tt><tt class="descname">executescript</tt><big>(</big><em>sql_script</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.executescript" title="Permalink to this definition"></a></dt>
<dd><p>This is a nonstandard shortcut that creates an intermediate cursor object by
calling the cursor method, then calls the cursor&#8217;s
<a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><tt class="xref py py-meth docutils literal"><span class="pre">executescript</span></tt></a> method with the parameters
given.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.create_function">
<tt class="descclassname">Connection.</tt><tt class="descname">create_function</tt><big>(</big><em>name</em>, <em>num_params</em>, <em>func</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.create_function" title="Permalink to this definition"></a></dt>
<dd><p>Creates a user-defined function that you can later use from within SQL
statements under the function name <em>name</em>. <em>num_params</em> is the number of
parameters the function accepts, and <em>func</em> is a Python callable that is called
as the SQL function.</p>
<p>The function can return any of the types supported by SQLite: unicode, str, int,
long, float, buffer and None.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>
<span class="kn">import</span> <span class="nn">md5</span>

<span class="k">def</span> <span class="nf">md5sum</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">md5</span><span class="o">.</span><span class="n">md5</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">.</span><span class="n">hexdigest</span><span class="p">()</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">create_function</span><span class="p">(</span><span class="s">&quot;md5&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">md5sum</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select md5(?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;foo&quot;</span><span class="p">,))</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.create_aggregate">
<tt class="descclassname">Connection.</tt><tt class="descname">create_aggregate</tt><big>(</big><em>name</em>, <em>num_params</em>, <em>aggregate_class</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.create_aggregate" title="Permalink to this definition"></a></dt>
<dd><p>Creates a user-defined aggregate function.</p>
<p>The aggregate class must implement a <tt class="docutils literal"><span class="pre">step</span></tt> method, which accepts the number
of parameters <em>num_params</em>, and a <tt class="docutils literal"><span class="pre">finalize</span></tt> method which will return the
final result of the aggregate.</p>
<p>The <tt class="docutils literal"><span class="pre">finalize</span></tt> method can return any of the types supported by SQLite:
unicode, str, int, long, float, buffer and None.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">class</span> <span class="nc">MySum</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">count</span> <span class="o">=</span> <span class="mi">0</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="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">finalize</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">count</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">create_aggregate</span><span class="p">(</span><span class="s">&quot;mysum&quot;</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">MySum</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table test(i)&quot;</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into test(i) values (1)&quot;</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into test(i) values (2)&quot;</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select mysum(i) from test&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.create_collation">
<tt class="descclassname">Connection.</tt><tt class="descname">create_collation</tt><big>(</big><em>name</em>, <em>callable</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.create_collation" title="Permalink to this definition"></a></dt>
<dd><p>Creates a collation with the specified <em>name</em> and <em>callable</em>. The callable will
be passed two string arguments. It should return -1 if the first is ordered
lower than the second, 0 if they are ordered equal and 1 if the first is ordered
higher than the second.  Note that this controls sorting (ORDER BY in SQL) so
your comparisons don&#8217;t affect other SQL operations.</p>
<p>Note that the callable will get its parameters as Python bytestrings, which will
normally be encoded in UTF-8.</p>
<p>The following example shows a custom collation that sorts &#8220;the wrong way&#8221;:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">def</span> <span class="nf">collate_reverse</span><span class="p">(</span><span class="n">string1</span><span class="p">,</span> <span class="n">string2</span><span class="p">):</span>
    <span class="k">return</span> <span class="o">-</span><span class="nb">cmp</span><span class="p">(</span><span class="n">string1</span><span class="p">,</span> <span class="n">string2</span><span class="p">)</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">create_collation</span><span class="p">(</span><span class="s">&quot;reverse&quot;</span><span class="p">,</span> <span class="n">collate_reverse</span><span class="p">)</span>

<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table test(x)&quot;</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">&quot;insert into test(x) values (?)&quot;</span><span class="p">,</span> <span class="p">[(</span><span class="s">&quot;a&quot;</span><span class="p">,),</span> <span class="p">(</span><span class="s">&quot;b&quot;</span><span class="p">,)])</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select x from test order by x collate reverse&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">row</span>
<span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>To remove a collation, call <tt class="docutils literal"><span class="pre">create_collation</span></tt> with None as callable:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">con</span><span class="o">.</span><span class="n">create_collation</span><span class="p">(</span><span class="s">&quot;reverse&quot;</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.interrupt">
<tt class="descclassname">Connection.</tt><tt class="descname">interrupt</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Connection.interrupt" title="Permalink to this definition"></a></dt>
<dd><p>You can call this method from a different thread to abort any queries that might
be executing on the connection. The query will then abort and the caller will
get an exception.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.set_authorizer">
<tt class="descclassname">Connection.</tt><tt class="descname">set_authorizer</tt><big>(</big><em>authorizer_callback</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.set_authorizer" title="Permalink to this definition"></a></dt>
<dd><p>This routine registers a callback. The callback is invoked for each attempt to
access a column of a table in the database. The callback should return
<tt class="xref py py-const docutils literal"><span class="pre">SQLITE_OK</span></tt> if access is allowed, <tt class="xref py py-const docutils literal"><span class="pre">SQLITE_DENY</span></tt> if the entire SQL
statement should be aborted with an error and <tt class="xref py py-const docutils literal"><span class="pre">SQLITE_IGNORE</span></tt> if the
column should be treated as a NULL value. These constants are available in the
<a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module.</p>
<p>The first argument to the callback signifies what kind of operation is to be
authorized. The second and third argument will be arguments or <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt>
depending on the first argument. The 4th argument is the name of the database
(&#8220;main&#8221;, &#8220;temp&#8221;, etc.) if applicable. The 5th argument is the name of the
inner-most trigger or view that is responsible for the access attempt or
<tt class="xref py py-const docutils literal"><span class="pre">None</span></tt> if this access attempt is directly from input SQL code.</p>
<p>Please consult the SQLite documentation about the possible values for the first
argument and the meaning of the second and third argument depending on the first
one. All necessary constants are available in the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.set_progress_handler">
<tt class="descclassname">Connection.</tt><tt class="descname">set_progress_handler</tt><big>(</big><em>handler</em>, <em>n</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.set_progress_handler" title="Permalink to this definition"></a></dt>
<dd><p>This routine registers a callback. The callback is invoked for every <em>n</em>
instructions of the SQLite virtual machine. This is useful if you want to
get called from SQLite during long-running operations, for example to update
a GUI.</p>
<p>If you want to clear any previously installed progress handler, call the
method with <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt> for <em>handler</em>.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.enable_load_extension">
<tt class="descclassname">Connection.</tt><tt class="descname">enable_load_extension</tt><big>(</big><em>enabled</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.enable_load_extension" title="Permalink to this definition"></a></dt>
<dd><p>This routine allows/disallows the SQLite engine to load SQLite extensions
from shared libraries.  SQLite extensions can define new functions,
aggregates or whole new virtual table implementations. One well-known
extension is the fulltext-search extension distributed with SQLite.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>

<span class="c"># enable extension loading</span>
<span class="n">con</span><span class="o">.</span><span class="n">enable_load_extension</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>

<span class="c"># Load the fulltext search extension</span>
<span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select load_extension(&#39;./fts3.so&#39;)&quot;</span><span class="p">)</span>

<span class="c"># alternatively you can load the extension using an API call:</span>
<span class="c"># con.load_extension(&quot;./fts3.so&quot;)</span>

<span class="c"># disable extension laoding again</span>
<span class="n">con</span><span class="o">.</span><span class="n">enable_load_extension</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>

<span class="c"># example from SQLite wiki</span>
<span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create virtual table recipe using fts3(name, ingredients)&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">executescript</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">    insert into recipe (name, ingredients) values (&#39;broccoli stew&#39;, &#39;broccoli peppers cheese tomatoes&#39;);</span>
<span class="s">    insert into recipe (name, ingredients) values (&#39;pumpkin stew&#39;, &#39;pumpkin onions garlic celery&#39;);</span>
<span class="s">    insert into recipe (name, ingredients) values (&#39;broccoli pie&#39;, &#39;broccoli cheese onions flour&#39;);</span>
<span class="s">    insert into recipe (name, ingredients) values (&#39;pumpkin pie&#39;, &#39;pumpkin sugar flour butter&#39;);</span>
<span class="s">    &quot;&quot;&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select rowid, name, ingredients from recipe where name match &#39;pie&#39;&quot;</span><span class="p">):</span>
    <span class="k">print</span> <span class="n">row</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Connection.load_extension">
<tt class="descclassname">Connection.</tt><tt class="descname">load_extension</tt><big>(</big><em>path</em><big>)</big><a class="headerlink" href="#sqlite3.Connection.load_extension" title="Permalink to this definition"></a></dt>
<dd><p>This routine loads a SQLite extension from a shared library. You have to
enable extension loading with <tt class="docutils literal"><span class="pre">enable_load_extension</span></tt> before you can use
this routine.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Connection.row_factory">
<tt class="descclassname">Connection.</tt><tt class="descname">row_factory</tt><a class="headerlink" href="#sqlite3.Connection.row_factory" title="Permalink to this definition"></a></dt>
<dd><p>You can change this attribute to a callable that accepts the cursor and the
original row as a tuple and will return the real result row.  This way, you can
implement more advanced ways of returning results, such  as returning an object
that can also access columns by name.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">def</span> <span class="nf">dict_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="n">d</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">col</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">description</span><span class="p">):</span>
        <span class="n">d</span><span class="p">[</span><span class="n">col</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">=</span> <span class="n">row</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
    <span class="k">return</span> <span class="n">d</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">dict_factory</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select 1 as a&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="s">&quot;a&quot;</span><span class="p">]</span>
</pre></div>
</div>
<p>If returning a tuple doesn&#8217;t suffice and you want name-based access to
columns, you should consider setting <a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><tt class="xref py py-attr docutils literal"><span class="pre">row_factory</span></tt></a> to the
highly-optimized <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">sqlite3.Row</span></tt></a> type. <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">Row</span></tt></a> provides both
index-based and case-insensitive name-based access to columns with almost no
memory overhead. It will probably be better than your own custom
dictionary-based approach or even a db_row based solution.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Connection.text_factory">
<tt class="descclassname">Connection.</tt><tt class="descname">text_factory</tt><a class="headerlink" href="#sqlite3.Connection.text_factory" title="Permalink to this definition"></a></dt>
<dd><p>Using this attribute you can control what objects are returned for the <tt class="docutils literal"><span class="pre">TEXT</span></tt>
data type. By default, this attribute is set to <tt class="xref py py-class docutils literal"><span class="pre">unicode</span></tt> and the
<a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module will return Unicode objects for <tt class="docutils literal"><span class="pre">TEXT</span></tt>. If you want to
return bytestrings instead, you can set it to <tt class="xref py py-class docutils literal"><span class="pre">str</span></tt>.</p>
<p>For efficiency reasons, there&#8217;s also a way to return Unicode objects only for
non-ASCII data, and bytestrings otherwise. To activate it, set this attribute to
<tt class="xref py py-const docutils literal"><span class="pre">sqlite3.OptimizedUnicode</span></tt>.</p>
<p>You can also set it to any other callable that accepts a single bytestring
parameter and returns the resulting object.</p>
<p>See the following example code for illustration:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="c"># Create the table</span>
<span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table person(lastname, firstname)&quot;</span><span class="p">)</span>

<span class="n">AUSTRIA</span> <span class="o">=</span> <span class="s">u&quot;</span><span class="se">\xd6</span><span class="s">sterreich&quot;</span>

<span class="c"># by default, rows are returned as Unicode</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">AUSTRIA</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">AUSTRIA</span>

<span class="c"># but we can make pysqlite always return bytestrings ...</span>
<span class="n">con</span><span class="o">.</span><span class="n">text_factory</span> <span class="o">=</span> <span class="nb">str</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">AUSTRIA</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="nb">str</span>
<span class="c"># the bytestrings will be encoded in UTF-8, unless you stored garbage in the</span>
<span class="c"># database ...</span>
<span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">AUSTRIA</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&quot;utf-8&quot;</span><span class="p">)</span>

<span class="c"># we can also implement a custom text_factory ...</span>
<span class="c"># here we implement one that will ignore Unicode characters that cannot be</span>
<span class="c"># decoded from UTF-8</span>
<span class="n">con</span><span class="o">.</span><span class="n">text_factory</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">unicode</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="s">&quot;utf-8&quot;</span><span class="p">,</span> <span class="s">&quot;ignore&quot;</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;this is latin1 and would normally create errors&quot;</span> <span class="o">+</span> <span class="s">u&quot;</span><span class="se">\xe4\xf6\xfc</span><span class="s">&quot;</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&quot;latin1&quot;</span><span class="p">),))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="nb">unicode</span>

<span class="c"># pysqlite offers a builtin optimized text_factory that will return bytestring</span>
<span class="c"># objects, if the data is in ASCII only, and otherwise return unicode objects</span>
<span class="n">con</span><span class="o">.</span><span class="n">text_factory</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">OptimizedUnicode</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">AUSTRIA</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="nb">unicode</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;Germany&quot;</span><span class="p">,))</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">assert</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">==</span> <span class="nb">str</span>
</pre></div>
</div>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Connection.total_changes">
<tt class="descclassname">Connection.</tt><tt class="descname">total_changes</tt><a class="headerlink" href="#sqlite3.Connection.total_changes" title="Permalink to this definition"></a></dt>
<dd><p>Returns the total number of database rows that have been modified, inserted, or
deleted since the database connection was opened.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Connection.iterdump">
<tt class="descclassname">Connection.</tt><tt class="descname">iterdump</tt><a class="headerlink" href="#sqlite3.Connection.iterdump" title="Permalink to this definition"></a></dt>
<dd><p>Returns an iterator to dump the database in an SQL text format.  Useful when
saving an in-memory database for later restoration.  This function provides
the same capabilities as the <tt class="kbd docutils literal"><span class="pre">.dump</span></tt> command in the <strong class="program">sqlite3</strong>
shell.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Convert file existing_db.db to SQL dump file dump.sql</span>
<span class="kn">import</span> <span class="nn">sqlite3</span><span class="o">,</span> <span class="nn">os</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&#39;existing_db.db&#39;</span><span class="p">)</span>
<span class="n">full_dump</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">linesep</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">iterdump</span><span class="p">()])</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;dump.sql&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">writelines</span><span class="p">(</span><span class="n">full_dump</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="cursor-objects">
<span id="sqlite3-cursor-objects"></span><h2>Cursor Objects<a class="headerlink" href="#cursor-objects" title="Permalink to this headline"></a></h2>
<p>A <tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt> instance has the following attributes and methods:</p>
<blockquote>
<div>A SQLite database cursor has the following attributes and methods:</div></blockquote>
<dl class="method">
<dt id="sqlite3.Cursor.execute">
<tt class="descclassname">Cursor.</tt><tt class="descname">execute</tt><big>(</big><em>sql</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.Cursor.execute" title="Permalink to this definition"></a></dt>
<dd><p>Executes an SQL statement. The SQL statement may be parametrized (i. e.
placeholders instead of SQL literals). The <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module supports two
kinds of placeholders: question marks (qmark style) and named placeholders
(named style).</p>
<p>This example shows how to use parameters with qmark style:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;mydb&quot;</span><span class="p">)</span>

<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">who</span> <span class="o">=</span> <span class="s">&quot;Yeltsin&quot;</span>
<span class="n">age</span> <span class="o">=</span> <span class="mi">72</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select name_last, age from people where name_last=? and age=?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">who</span><span class="p">,</span> <span class="n">age</span><span class="p">))</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
</pre></div>
</div>
<p>This example shows how to use the named style:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;mydb&quot;</span><span class="p">)</span>

<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">who</span> <span class="o">=</span> <span class="s">&quot;Yeltsin&quot;</span>
<span class="n">age</span> <span class="o">=</span> <span class="mi">72</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select name_last, age from people where name_last=:who and age=:age&quot;</span><span class="p">,</span>
    <span class="p">{</span><span class="s">&quot;who&quot;</span><span class="p">:</span> <span class="n">who</span><span class="p">,</span> <span class="s">&quot;age&quot;</span><span class="p">:</span> <span class="n">age</span><span class="p">})</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
</pre></div>
</div>
<p><a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><tt class="xref py py-meth docutils literal"><span class="pre">execute()</span></tt></a> will only execute a single SQL statement. If you try to execute
more than one statement with it, it will raise a Warning. Use
<a class="reference internal" href="#sqlite3.Cursor.executescript" title="sqlite3.Cursor.executescript"><tt class="xref py py-meth docutils literal"><span class="pre">executescript()</span></tt></a> if you want to execute multiple SQL statements with one
call.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Cursor.executemany">
<tt class="descclassname">Cursor.</tt><tt class="descname">executemany</tt><big>(</big><em>sql</em>, <em>seq_of_parameters</em><big>)</big><a class="headerlink" href="#sqlite3.Cursor.executemany" title="Permalink to this definition"></a></dt>
<dd><p>Executes an SQL command against all parameter sequences or mappings found in
the sequence <em>sql</em>.  The <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module also allows using an
<em class="xref std std-term">iterator</em> yielding parameters instead of a sequence.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">class</span> <span class="nc">IterChars</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">count</span> <span class="o">=</span> <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;a&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__iter__</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="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">&gt;</span> <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;z&#39;</span><span class="p">):</span>
            <span class="k">raise</span> <span class="ne">StopIteration</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="p">(</span><span class="nb">chr</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),)</span> <span class="c"># this is a 1-tuple</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table characters(c)&quot;</span><span class="p">)</span>

<span class="n">theIter</span> <span class="o">=</span> <span class="n">IterChars</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">&quot;insert into characters(c) values (?)&quot;</span><span class="p">,</span> <span class="n">theIter</span><span class="p">)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select c from characters&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
</pre></div>
</div>
<p>Here&#8217;s a shorter example using a <em class="xref std std-term">generator</em>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">def</span> <span class="nf">char_generator</span><span class="p">():</span>
    <span class="kn">import</span> <span class="nn">string</span>
    <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">string</span><span class="o">.</span><span class="n">letters</span><span class="p">[:</span><span class="mi">26</span><span class="p">]:</span>
        <span class="k">yield</span> <span class="p">(</span><span class="n">c</span><span class="p">,)</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table characters(c)&quot;</span><span class="p">)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">&quot;insert into characters(c) values (?)&quot;</span><span class="p">,</span> <span class="n">char_generator</span><span class="p">())</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select c from characters&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchall</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Cursor.executescript">
<tt class="descclassname">Cursor.</tt><tt class="descname">executescript</tt><big>(</big><em>sql_script</em><big>)</big><a class="headerlink" href="#sqlite3.Cursor.executescript" title="Permalink to this definition"></a></dt>
<dd><p>This is a nonstandard convenience method for executing multiple SQL statements
at once. It issues a <tt class="docutils literal"><span class="pre">COMMIT</span></tt> statement first, then executes the SQL script it
gets as a parameter.</p>
<p><em>sql_script</em> can be a bytestring or a Unicode string.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">executescript</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">    create table person(</span>
<span class="s">        firstname,</span>
<span class="s">        lastname,</span>
<span class="s">        age</span>
<span class="s">    );</span>

<span class="s">    create table book(</span>
<span class="s">        title,</span>
<span class="s">        author,</span>
<span class="s">        published</span>
<span class="s">    );</span>

<span class="s">    insert into book(title, author, published)</span>
<span class="s">    values (</span>
<span class="s">        &#39;Dirk Gently&#39;&#39;s Holistic Detective Agency&#39;,</span>
<span class="s">        &#39;Douglas Adams&#39;,</span>
<span class="s">        1987</span>
<span class="s">    );</span>
<span class="s">    &quot;&quot;&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Cursor.fetchone">
<tt class="descclassname">Cursor.</tt><tt class="descname">fetchone</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Cursor.fetchone" title="Permalink to this definition"></a></dt>
<dd><p>Fetches the next row of a query result set, returning a single sequence,
or <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt> when no more data is available.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Cursor.fetchmany">
<tt class="descclassname">Cursor.</tt><tt class="descname">fetchmany</tt><big>(</big><span class="optional">[</span><em>size=cursor.arraysize</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sqlite3.Cursor.fetchmany" title="Permalink to this definition"></a></dt>
<dd><p>Fetches the next set of rows of a query result, returning a list.  An empty
list is returned when no more rows are available.</p>
<p>The number of rows to fetch per call is specified by the <em>size</em> parameter.
If it is not given, the cursor&#8217;s arraysize determines the number of rows
to be fetched. The method should try to fetch as many rows as indicated by
the size parameter. If this is not possible due to the specified number of
rows not being available, fewer rows may be returned.</p>
<p>Note there are performance considerations involved with the <em>size</em> parameter.
For optimal performance, it is usually best to use the arraysize attribute.
If the <em>size</em> parameter is used, then it is best for it to retain the same
value from one <a class="reference internal" href="#sqlite3.Cursor.fetchmany" title="sqlite3.Cursor.fetchmany"><tt class="xref py py-meth docutils literal"><span class="pre">fetchmany()</span></tt></a> call to the next.</p>
</dd></dl>

<dl class="method">
<dt id="sqlite3.Cursor.fetchall">
<tt class="descclassname">Cursor.</tt><tt class="descname">fetchall</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Cursor.fetchall" title="Permalink to this definition"></a></dt>
<dd><p>Fetches all (remaining) rows of a query result, returning a list.  Note that
the cursor&#8217;s arraysize attribute can affect the performance of this operation.
An empty list is returned when no rows are available.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Cursor.rowcount">
<tt class="descclassname">Cursor.</tt><tt class="descname">rowcount</tt><a class="headerlink" href="#sqlite3.Cursor.rowcount" title="Permalink to this definition"></a></dt>
<dd><p>Although the <tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt> class of the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module implements this
attribute, the database engine&#8217;s own support for the determination of &#8220;rows
affected&#8221;/&#8221;rows selected&#8221; is quirky.</p>
<p>For <tt class="docutils literal"><span class="pre">DELETE</span></tt> statements, SQLite reports <a class="reference internal" href="#sqlite3.Cursor.rowcount" title="sqlite3.Cursor.rowcount"><tt class="xref py py-attr docutils literal"><span class="pre">rowcount</span></tt></a> as 0 if you make a
<tt class="docutils literal"><span class="pre">DELETE</span> <span class="pre">FROM</span> <span class="pre">table</span></tt> without any condition.</p>
<p>For <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><tt class="xref py py-meth docutils literal"><span class="pre">executemany()</span></tt></a> statements, the number of modifications are summed up
into <a class="reference internal" href="#sqlite3.Cursor.rowcount" title="sqlite3.Cursor.rowcount"><tt class="xref py py-attr docutils literal"><span class="pre">rowcount</span></tt></a>.</p>
<p>As required by the Python DB API Spec, the <a class="reference internal" href="#sqlite3.Cursor.rowcount" title="sqlite3.Cursor.rowcount"><tt class="xref py py-attr docutils literal"><span class="pre">rowcount</span></tt></a> attribute &#8220;is -1 in
case no <tt class="docutils literal"><span class="pre">executeXX()</span></tt> has been performed on the cursor or the rowcount of the
last operation is not determinable by the interface&#8221;.</p>
<p>This includes <tt class="docutils literal"><span class="pre">SELECT</span></tt> statements because we cannot determine the number of
rows a query produced until all rows were fetched.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Cursor.lastrowid">
<tt class="descclassname">Cursor.</tt><tt class="descname">lastrowid</tt><a class="headerlink" href="#sqlite3.Cursor.lastrowid" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute provides the rowid of the last modified row. It is
only set if you issued a <tt class="docutils literal"><span class="pre">INSERT</span></tt> statement using the <a class="reference internal" href="#sqlite3.Cursor.execute" title="sqlite3.Cursor.execute"><tt class="xref py py-meth docutils literal"><span class="pre">execute()</span></tt></a>
method. For operations other than <tt class="docutils literal"><span class="pre">INSERT</span></tt> or when <a class="reference internal" href="#sqlite3.Cursor.executemany" title="sqlite3.Cursor.executemany"><tt class="xref py py-meth docutils literal"><span class="pre">executemany()</span></tt></a> is
called, <a class="reference internal" href="#sqlite3.Cursor.lastrowid" title="sqlite3.Cursor.lastrowid"><tt class="xref py py-attr docutils literal"><span class="pre">lastrowid</span></tt></a> is set to <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt>.</p>
</dd></dl>

<dl class="attribute">
<dt id="sqlite3.Cursor.description">
<tt class="descclassname">Cursor.</tt><tt class="descname">description</tt><a class="headerlink" href="#sqlite3.Cursor.description" title="Permalink to this definition"></a></dt>
<dd><p>This read-only attribute provides the column names of the last query. To
remain compatible with the Python DB API, it returns a 7-tuple for each
column where the last six items of each tuple are <tt class="xref py py-const docutils literal"><span class="pre">None</span></tt>.</p>
<p>It is set for <tt class="docutils literal"><span class="pre">SELECT</span></tt> statements without any matching rows as well.</p>
</dd></dl>

</div>
<div class="section" id="row-objects">
<span id="sqlite3-row-objects"></span><h2>Row Objects<a class="headerlink" href="#row-objects" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="sqlite3.Row">
<em class="property">class </em><tt class="descclassname">sqlite3.</tt><tt class="descname">Row</tt><a class="headerlink" href="#sqlite3.Row" title="Permalink to this definition"></a></dt>
<dd><p>A <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">Row</span></tt></a> instance serves as a highly optimized
<a class="reference internal" href="#sqlite3.Connection.row_factory" title="sqlite3.Connection.row_factory"><tt class="xref py py-attr docutils literal"><span class="pre">row_factory</span></tt></a> for <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> objects.
It tries to mimic a tuple in most of its features.</p>
<p>It supports mapping access by column name and index, iteration,
representation, equality testing and <tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt>.</p>
<p>If two <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">Row</span></tt></a> objects have exactly the same columns and their
members are equal, they compare equal.</p>
<p class="versionchanged">
<span class="versionmodified">Changed in version 2.6: </span>Added iteration and equality (hashability).</p>
<dl class="method">
<dt id="sqlite3.Row.keys">
<tt class="descname">keys</tt><big>(</big><big>)</big><a class="headerlink" href="#sqlite3.Row.keys" title="Permalink to this definition"></a></dt>
<dd><p>This method returns a tuple of column names. Immediately after a query,
it is the first member of each tuple in <a class="reference internal" href="#sqlite3.Cursor.description" title="sqlite3.Cursor.description"><tt class="xref py py-attr docutils literal"><span class="pre">Cursor.description</span></tt></a>.</p>
<p class="versionadded">
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

</dd></dl>

<p>Let&#8217;s assume we initialize a table as in the example given above:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">conn</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;&#39;&#39;create table stocks</span>
<span class="s">(date text, trans text, symbol text,</span>
<span class="s"> qty real, price real)&#39;&#39;&#39;</span><span class="p">)</span>
<span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;&quot;&quot;insert into stocks</span>
<span class="s">          values (&#39;2006-01-05&#39;,&#39;BUY&#39;,&#39;RHAT&#39;,100,35.14)&quot;&quot;&quot;</span><span class="p">)</span>
<span class="n">conn</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
<span class="n">c</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Now we plug <a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">Row</span></tt></a> in:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Row</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;select * from stocks&#39;</span><span class="p">)</span>
<span class="go">&lt;sqlite3.Cursor object at 0x7f4e7dd8fa80&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">type</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="go">&lt;type &#39;sqlite3.Row&#39;&gt;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span>
<span class="go">(u&#39;2006-01-05&#39;, u&#39;BUY&#39;, u&#39;RHAT&#39;, 100.0, 35.14)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="go">5</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="go">u&#39;RHAT&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
<span class="go">[&#39;date&#39;, &#39;trans&#39;, &#39;symbol&#39;, &#39;qty&#39;, &#39;price&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r</span><span class="p">[</span><span class="s">&#39;qty&#39;</span><span class="p">]</span>
<span class="go">100.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">for</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">r</span><span class="p">:</span> <span class="k">print</span> <span class="n">member</span>
<span class="gp">...</span>
<span class="go">2006-01-05</span>
<span class="go">BUY</span>
<span class="go">RHAT</span>
<span class="go">100.0</span>
<span class="go">35.14</span>
</pre></div>
</div>
</div>
<div class="section" id="sqlite-and-python-types">
<span id="sqlite3-types"></span><h2>SQLite and Python types<a class="headerlink" href="#sqlite-and-python-types" title="Permalink to this headline"></a></h2>
<div class="section" id="introduction">
<h3>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h3>
<p>SQLite natively supports the following types: <tt class="docutils literal"><span class="pre">NULL</span></tt>, <tt class="docutils literal"><span class="pre">INTEGER</span></tt>,
<tt class="docutils literal"><span class="pre">REAL</span></tt>, <tt class="docutils literal"><span class="pre">TEXT</span></tt>, <tt class="docutils literal"><span class="pre">BLOB</span></tt>.</p>
<p>The following Python types can thus be sent to SQLite without any problem:</p>
<table border="1" class="docutils">
<colgroup>
<col width="69%" />
<col width="31%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Python type</th>
<th class="head">SQLite type</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></td>
<td><tt class="docutils literal"><span class="pre">NULL</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></td>
<td><tt class="docutils literal"><span class="pre">INTEGER</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-class docutils literal"><span class="pre">long</span></tt></td>
<td><tt class="docutils literal"><span class="pre">INTEGER</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></td>
<td><tt class="docutils literal"><span class="pre">REAL</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt> (UTF8-encoded)</td>
<td><tt class="docutils literal"><span class="pre">TEXT</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="xref py py-class docutils literal"><span class="pre">unicode</span></tt></td>
<td><tt class="docutils literal"><span class="pre">TEXT</span></tt></td>
</tr>
<tr class="row-even"><td><tt class="xref py py-class docutils literal"><span class="pre">buffer</span></tt></td>
<td><tt class="docutils literal"><span class="pre">BLOB</span></tt></td>
</tr>
</tbody>
</table>
<p>This is how SQLite types are converted to Python types by default:</p>
<table border="1" class="docutils">
<colgroup>
<col width="22%" />
<col width="78%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">SQLite type</th>
<th class="head">Python type</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">NULL</span></tt></td>
<td><tt class="xref py py-const docutils literal"><span class="pre">None</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">INTEGER</span></tt></td>
<td><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt> or <tt class="xref py py-class docutils literal"><span class="pre">long</span></tt>,
depending on size</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">REAL</span></tt></td>
<td><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">TEXT</span></tt></td>
<td>depends on <a class="reference internal" href="#sqlite3.Connection.text_factory" title="sqlite3.Connection.text_factory"><tt class="xref py py-attr docutils literal"><span class="pre">text_factory</span></tt></a>,
<tt class="xref py py-class docutils literal"><span class="pre">unicode</span></tt> by default</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">BLOB</span></tt></td>
<td><tt class="xref py py-class docutils literal"><span class="pre">buffer</span></tt></td>
</tr>
</tbody>
</table>
<p>The type system of the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module is extensible in two ways: you can
store additional Python types in a SQLite database via object adaptation, and
you can let the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module convert SQLite types to different Python
types via converters.</p>
</div>
<div class="section" id="using-adapters-to-store-additional-python-types-in-sqlite-databases">
<h3>Using adapters to store additional Python types in SQLite databases<a class="headerlink" href="#using-adapters-to-store-additional-python-types-in-sqlite-databases" title="Permalink to this headline"></a></h3>
<p>As described before, SQLite supports only a limited set of types natively. To
use other Python types with SQLite, you must <strong>adapt</strong> them to one of the
sqlite3 module&#8217;s supported types for SQLite: one of NoneType, int, long, float,
str, unicode, buffer.</p>
<p>The <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module uses Python object adaptation, as described in
<span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0246"><strong>PEP 246</strong></a> for this.  The protocol to use is <tt class="xref py py-class docutils literal"><span class="pre">PrepareProtocol</span></tt>.</p>
<p>There are two ways to enable the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module to adapt a custom Python
type to one of the supported ones.</p>
<div class="section" id="letting-your-object-adapt-itself">
<h4>Letting your object adapt itself<a class="headerlink" href="#letting-your-object-adapt-itself" title="Permalink to this headline"></a></h4>
<p>This is a good approach if you write the class yourself. Let&#8217;s suppose you have
a class like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>
</pre></div>
</div>
<p>Now you want to store the point in a single SQLite column.  First you&#8217;ll have to
choose one of the supported types first to be used for representing the point.
Let&#8217;s just use str and separate the coordinates using a semicolon. Then you need
to give your class a method <tt class="docutils literal"><span class="pre">__conform__(self,</span> <span class="pre">protocol)</span></tt> which must return
the converted value. The parameter <em>protocol</em> will be <tt class="xref py py-class docutils literal"><span class="pre">PrepareProtocol</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>

    <span class="k">def</span> <span class="nf">__conform__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">protocol</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">protocol</span> <span class="ow">is</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">PrepareProtocol</span><span class="p">:</span>
            <span class="k">return</span> <span class="s">&quot;</span><span class="si">%f</span><span class="s">;</span><span class="si">%f</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="registering-an-adapter-callable">
<h4>Registering an adapter callable<a class="headerlink" href="#registering-an-adapter-callable" title="Permalink to this headline"></a></h4>
<p>The other possibility is to create a function that converts the type to the
string representation and register the function with <a class="reference internal" href="#sqlite3.register_adapter" title="sqlite3.register_adapter"><tt class="xref py py-meth docutils literal"><span class="pre">register_adapter()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The type/class to adapt must be a <em class="xref std std-term">new-style class</em>, i. e. it must have
<tt class="xref py py-class docutils literal"><span class="pre">object</span></tt> as one of its bases.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>

<span class="k">def</span> <span class="nf">adapt_point</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
    <span class="k">return</span> <span class="s">&quot;</span><span class="si">%f</span><span class="s">;</span><span class="si">%f</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">point</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>

<span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span> <span class="n">adapt_point</span><span class="p">)</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">)</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module has two default adapters for Python&#8217;s built-in
<tt class="xref py py-class docutils literal"><span class="pre">datetime.date</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt> types.  Now let&#8217;s suppose
we want to store <tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt> objects not in ISO representation,
but as a Unix timestamp.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>
<span class="kn">import</span> <span class="nn">datetime</span><span class="o">,</span> <span class="nn">time</span>

<span class="k">def</span> <span class="nf">adapt_datetime</span><span class="p">(</span><span class="n">ts</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">time</span><span class="o">.</span><span class="n">mktime</span><span class="p">(</span><span class="n">ts</span><span class="o">.</span><span class="n">timetuple</span><span class="p">())</span>

<span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="p">,</span> <span class="n">adapt_datetime</span><span class="p">)</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>

<span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select ?&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">now</span><span class="p">,))</span>
<span class="k">print</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="converting-sqlite-values-to-custom-python-types">
<h3>Converting SQLite values to custom Python types<a class="headerlink" href="#converting-sqlite-values-to-custom-python-types" title="Permalink to this headline"></a></h3>
<p>Writing an adapter lets you send custom Python types to SQLite. But to make it
really useful we need to make the Python to SQLite to Python roundtrip work.</p>
<p>Enter converters.</p>
<p>Let&#8217;s go back to the <tt class="xref py py-class docutils literal"><span class="pre">Point</span></tt> class. We stored the x and y coordinates
separated via semicolons as strings in SQLite.</p>
<p>First, we&#8217;ll define a converter function that accepts the string as a parameter
and constructs a <tt class="xref py py-class docutils literal"><span class="pre">Point</span></tt> object from it.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Converter functions <strong>always</strong> get called with a string, no matter under which
data type you sent the value to SQLite.</p>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">convert_point</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;;&quot;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
</div>
<p>Now you need to make the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module know that what you select from
the database is actually a point. There are two ways of doing this:</p>
<ul class="simple">
<li>Implicitly via the declared type</li>
<li>Explicitly via the column name</li>
</ul>
<p>Both ways are described in section <a class="reference internal" href="#sqlite3-module-contents"><em>Module functions and constants</em></a>, in the entries
for the constants <a class="reference internal" href="#sqlite3.PARSE_DECLTYPES" title="sqlite3.PARSE_DECLTYPES"><tt class="xref py py-const docutils literal"><span class="pre">PARSE_DECLTYPES</span></tt></a> and <a class="reference internal" href="#sqlite3.PARSE_COLNAMES" title="sqlite3.PARSE_COLNAMES"><tt class="xref py py-const docutils literal"><span class="pre">PARSE_COLNAMES</span></tt></a>.</p>
<p>The following example illustrates both approaches.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="k">class</span> <span class="nc">Point</span><span class="p">(</span><span class="nb">object</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">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&quot;(</span><span class="si">%f</span><span class="s">;</span><span class="si">%f</span><span class="s">)&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">adapt_point</span><span class="p">(</span><span class="n">point</span><span class="p">):</span>
    <span class="k">return</span> <span class="s">&quot;</span><span class="si">%f</span><span class="s">;</span><span class="si">%f</span><span class="s">&quot;</span> <span class="o">%</span> <span class="p">(</span><span class="n">point</span><span class="o">.</span><span class="n">x</span><span class="p">,</span> <span class="n">point</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">convert_point</span><span class="p">(</span><span class="n">s</span><span class="p">):</span>
    <span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">float</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;;&quot;</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">Point</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>

<span class="c"># Register the adapter</span>
<span class="n">sqlite3</span><span class="o">.</span><span class="n">register_adapter</span><span class="p">(</span><span class="n">Point</span><span class="p">,</span> <span class="n">adapt_point</span><span class="p">)</span>

<span class="c"># Register the converter</span>
<span class="n">sqlite3</span><span class="o">.</span><span class="n">register_converter</span><span class="p">(</span><span class="s">&quot;point&quot;</span><span class="p">,</span> <span class="n">convert_point</span><span class="p">)</span>

<span class="n">p</span> <span class="o">=</span> <span class="n">Point</span><span class="p">(</span><span class="mf">4.0</span><span class="p">,</span> <span class="o">-</span><span class="mf">3.2</span><span class="p">)</span>

<span class="c">#########################</span>
<span class="c"># 1) Using declared types</span>
<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">,</span> <span class="n">detect_types</span><span class="o">=</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_DECLTYPES</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table test(p point)&quot;</span><span class="p">)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into test(p) values (?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select p from test&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;with declared types:&quot;</span><span class="p">,</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

<span class="c">#######################</span>
<span class="c"># 1) Using column names</span>
<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">,</span> <span class="n">detect_types</span><span class="o">=</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_COLNAMES</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table test(p)&quot;</span><span class="p">)</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into test(p) values (?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">p</span><span class="p">,))</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;select p as &quot;p [point]&quot; from test&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="s">&quot;with column names:&quot;</span><span class="p">,</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">cur</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">con</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="default-adapters-and-converters">
<h3>Default adapters and converters<a class="headerlink" href="#default-adapters-and-converters" title="Permalink to this headline"></a></h3>
<p>There are default adapters for the date and datetime types in the datetime
module. They will be sent as ISO dates/ISO timestamps to SQLite.</p>
<p>The default converters are registered under the name &#8220;date&#8221; for
<tt class="xref py py-class docutils literal"><span class="pre">datetime.date</span></tt> and under the name &#8220;timestamp&#8221; for
<tt class="xref py py-class docutils literal"><span class="pre">datetime.datetime</span></tt>.</p>
<p>This way, you can use date/timestamps from Python without any additional
fiddling in most cases. The format of the adapters is also compatible with the
experimental SQLite date/time functions.</p>
<p>The following example demonstrates this.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>
<span class="kn">import</span> <span class="nn">datetime</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">,</span> <span class="n">detect_types</span><span class="o">=</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_DECLTYPES</span><span class="o">|</span><span class="n">sqlite3</span><span class="o">.</span><span class="n">PARSE_COLNAMES</span><span class="p">)</span>
<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table test(d date, ts timestamp)&quot;</span><span class="p">)</span>

<span class="n">today</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">date</span><span class="o">.</span><span class="n">today</span><span class="p">()</span>
<span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into test(d, ts) values (?, ?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="n">today</span><span class="p">,</span> <span class="n">now</span><span class="p">))</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select d, ts from test&quot;</span><span class="p">)</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">print</span> <span class="n">today</span><span class="p">,</span> <span class="s">&quot;=&gt;&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="n">now</span><span class="p">,</span> <span class="s">&quot;=&gt;&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>

<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;select current_date as &quot;d [date]&quot;, current_timestamp as &quot;ts [timestamp]&quot;&#39;</span><span class="p">)</span>
<span class="n">row</span> <span class="o">=</span> <span class="n">cur</span><span class="o">.</span><span class="n">fetchone</span><span class="p">()</span>
<span class="k">print</span> <span class="s">&quot;current_date&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="k">print</span> <span class="s">&quot;current_timestamp&quot;</span><span class="p">,</span> <span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="controlling-transactions">
<span id="sqlite3-controlling-transactions"></span><h2>Controlling Transactions<a class="headerlink" href="#controlling-transactions" title="Permalink to this headline"></a></h2>
<p>By default, the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module opens transactions implicitly before a
Data Modification Language (DML)  statement (i.e.
<tt class="docutils literal"><span class="pre">INSERT</span></tt>/<tt class="docutils literal"><span class="pre">UPDATE</span></tt>/<tt class="docutils literal"><span class="pre">DELETE</span></tt>/<tt class="docutils literal"><span class="pre">REPLACE</span></tt>), and commits transactions
implicitly before a non-DML, non-query statement (i. e.
anything other than <tt class="docutils literal"><span class="pre">SELECT</span></tt> or the aforementioned).</p>
<p>So if you are within a transaction and issue a command like <tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span>
<span class="pre">...</span></tt>, <tt class="docutils literal"><span class="pre">VACUUM</span></tt>, <tt class="docutils literal"><span class="pre">PRAGMA</span></tt>, the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module will commit implicitly
before executing that command. There are two reasons for doing that. The first
is that some of these commands don&#8217;t work within transactions. The other reason
is that pysqlite needs to keep track of the transaction state (if a transaction
is active or not).</p>
<p>You can control which kind of <tt class="docutils literal"><span class="pre">BEGIN</span></tt> statements sqlite3 implicitly executes
(or none at all) via the <em>isolation_level</em> parameter to the <a class="reference internal" href="#sqlite3.connect" title="sqlite3.connect"><tt class="xref py py-func docutils literal"><span class="pre">connect()</span></tt></a>
call, or via the <tt class="xref py py-attr docutils literal"><span class="pre">isolation_level</span></tt> property of connections.</p>
<p>If you want <strong>autocommit mode</strong>, then set <tt class="xref py py-attr docutils literal"><span class="pre">isolation_level</span></tt> to None.</p>
<p>Otherwise leave it at its default, which will result in a plain &#8220;BEGIN&#8221;
statement, or set it to one of SQLite&#8217;s supported isolation levels: &#8220;DEFERRED&#8221;,
&#8220;IMMEDIATE&#8221; or &#8220;EXCLUSIVE&#8221;.</p>
</div>
<div class="section" id="using-sqlite3-efficiently">
<h2>Using <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> efficiently<a class="headerlink" href="#using-sqlite3-efficiently" title="Permalink to this headline"></a></h2>
<div class="section" id="using-shortcut-methods">
<h3>Using shortcut methods<a class="headerlink" href="#using-shortcut-methods" title="Permalink to this headline"></a></h3>
<p>Using the nonstandard <tt class="xref py py-meth docutils literal"><span class="pre">execute()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">executemany()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">executescript()</span></tt> methods of the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object, your code can
be written more concisely because you don&#8217;t have to create the (often
superfluous) <tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt> objects explicitly. Instead, the <tt class="xref py py-class docutils literal"><span class="pre">Cursor</span></tt>
objects are created implicitly and these shortcut methods return the cursor
objects. This way, you can execute a <tt class="docutils literal"><span class="pre">SELECT</span></tt> statement and iterate over it
directly using only a single call on the <a class="reference internal" href="#sqlite3.Connection" title="sqlite3.Connection"><tt class="xref py py-class docutils literal"><span class="pre">Connection</span></tt></a> object.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">persons</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s">&quot;Hugo&quot;</span><span class="p">,</span> <span class="s">&quot;Boss&quot;</span><span class="p">),</span>
    <span class="p">(</span><span class="s">&quot;Calvin&quot;</span><span class="p">,</span> <span class="s">&quot;Klein&quot;</span><span class="p">)</span>
    <span class="p">]</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>

<span class="c"># Create the table</span>
<span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table person(firstname, lastname)&quot;</span><span class="p">)</span>

<span class="c"># Fill the table</span>
<span class="n">con</span><span class="o">.</span><span class="n">executemany</span><span class="p">(</span><span class="s">&quot;insert into person(firstname, lastname) values (?, ?)&quot;</span><span class="p">,</span> <span class="n">persons</span><span class="p">)</span>

<span class="c"># Print the table contents</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select firstname, lastname from person&quot;</span><span class="p">):</span>
    <span class="k">print</span> <span class="n">row</span>

<span class="c"># Using a dummy WHERE clause to not let SQLite take the shortcut table deletes.</span>
<span class="k">print</span> <span class="s">&quot;I just deleted&quot;</span><span class="p">,</span> <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;delete from person where 1=1&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">rowcount</span><span class="p">,</span> <span class="s">&quot;rows&quot;</span>
</pre></div>
</div>
</div>
<div class="section" id="accessing-columns-by-name-instead-of-by-index">
<h3>Accessing columns by name instead of by index<a class="headerlink" href="#accessing-columns-by-name-instead-of-by-index" title="Permalink to this headline"></a></h3>
<p>One useful feature of the <a class="reference internal" href="#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module is the built-in
<a class="reference internal" href="#sqlite3.Row" title="sqlite3.Row"><tt class="xref py py-class docutils literal"><span class="pre">sqlite3.Row</span></tt></a> class designed to be used as a row factory.</p>
<p>Rows wrapped with this class can be accessed both by index (like tuples) and
case-insensitively by name:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;mydb&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">row_factory</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">Row</span>

<span class="n">cur</span> <span class="o">=</span> <span class="n">con</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
<span class="n">cur</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;select name_last, age from people&quot;</span><span class="p">)</span>
<span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">cur</span><span class="p">:</span>
    <span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="n">row</span><span class="p">[</span><span class="s">&quot;name_last&quot;</span><span class="p">]</span>
    <span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="s">&quot;name_last&quot;</span><span class="p">]</span> <span class="o">==</span> <span class="n">row</span><span class="p">[</span><span class="s">&quot;nAmE_lAsT&quot;</span><span class="p">]</span>
    <span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">row</span><span class="p">[</span><span class="s">&quot;age&quot;</span><span class="p">]</span>
    <span class="k">assert</span> <span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">row</span><span class="p">[</span><span class="s">&quot;AgE&quot;</span><span class="p">]</span>
</pre></div>
</div>
</div>
<div class="section" id="using-the-connection-as-a-context-manager">
<h3>Using the connection as a context manager<a class="headerlink" href="#using-the-connection-as-a-context-manager" title="Permalink to this headline"></a></h3>
<p>With Python 2.5 or higher, connection objects can be used as context managers
that automatically commit or rollback transactions.  In the event of an
exception, the transaction is rolled back; otherwise, the transaction is
committed:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">with_statement</span>
<span class="kn">from</span> <span class="nn">pysqlite2</span> <span class="kn">import</span> <span class="n">dbapi2</span> <span class="k">as</span> <span class="n">sqlite3</span>

<span class="n">con</span> <span class="o">=</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="s">&quot;:memory:&quot;</span><span class="p">)</span>
<span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;create table person (id integer primary key, firstname varchar unique)&quot;</span><span class="p">)</span>

<span class="c"># Successful, con.commit() is called automatically afterwards</span>
<span class="k">with</span> <span class="n">con</span><span class="p">:</span>
    <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into person(firstname) values (?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;Joe&quot;</span><span class="p">,))</span>

<span class="c"># con.rollback() is called after the with block finishes with an exception, the</span>
<span class="c"># exception is still raised and must be catched</span>
<span class="k">try</span><span class="p">:</span>
    <span class="k">with</span> <span class="n">con</span><span class="p">:</span>
        <span class="n">con</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&quot;insert into person(firstname) values (?)&quot;</span><span class="p">,</span> <span class="p">(</span><span class="s">&quot;Joe&quot;</span><span class="p">,))</span>
<span class="k">except</span> <span class="n">sqlite3</span><span class="o">.</span><span class="n">IntegrityError</span><span class="p">:</span>
    <span class="k">print</span> <span class="s">&quot;couldn&#39;t add Joe twice&quot;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="common-issues">
<h2>Common issues<a class="headerlink" href="#common-issues" title="Permalink to this headline"></a></h2>
<div class="section" id="multithreading">
<h3>Multithreading<a class="headerlink" href="#multithreading" title="Permalink to this headline"></a></h3>
<p>Older SQLite versions had issues with sharing connections between threads.
That&#8217;s why the Python module disallows sharing connections and cursors between
threads. If you still try to do so, you will get an exception at runtime.</p>
<p>The only exception is calling the <a class="reference internal" href="#sqlite3.Connection.interrupt" title="sqlite3.Connection.interrupt"><tt class="xref py py-meth docutils literal"><span class="pre">interrupt()</span></tt></a> method, which
only makes sense to call from a different thread.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><tt class="docutils literal"><span class="pre">sqlite3</span></tt> &#8212; DB-API 2.0 interface for SQLite databases</a><ul>
<li><a class="reference internal" href="#module-functions-and-constants">Module functions and constants</a></li>
<li><a class="reference internal" href="#connection-objects">Connection Objects</a></li>
<li><a class="reference internal" href="#cursor-objects">Cursor Objects</a></li>
<li><a class="reference internal" href="#row-objects">Row Objects</a></li>
<li><a class="reference internal" href="#sqlite-and-python-types">SQLite and Python types</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#using-adapters-to-store-additional-python-types-in-sqlite-databases">Using adapters to store additional Python types in SQLite databases</a><ul>
<li><a class="reference internal" href="#letting-your-object-adapt-itself">Letting your object adapt itself</a></li>
<li><a class="reference internal" href="#registering-an-adapter-callable">Registering an adapter callable</a></li>
</ul>
</li>
<li><a class="reference internal" href="#converting-sqlite-values-to-custom-python-types">Converting SQLite values to custom Python types</a></li>
<li><a class="reference internal" href="#default-adapters-and-converters">Default adapters and converters</a></li>
</ul>
</li>
<li><a class="reference internal" href="#controlling-transactions">Controlling Transactions</a></li>
<li><a class="reference internal" href="#using-sqlite3-efficiently">Using <tt class="docutils literal"><span class="pre">sqlite3</span></tt> efficiently</a><ul>
<li><a class="reference internal" href="#using-shortcut-methods">Using shortcut methods</a></li>
<li><a class="reference internal" href="#accessing-columns-by-name-instead-of-by-index">Accessing columns by name instead of by index</a></li>
<li><a class="reference internal" href="#using-the-connection-as-a-context-manager">Using the connection as a context manager</a></li>
</ul>
</li>
<li><a class="reference internal" href="#common-issues">Common issues</a><ul>
<li><a class="reference internal" href="#multithreading">Multithreading</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/sqlite3.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" />
      <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><a href="index.html">pysqlite 2.6.0 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
        &copy; Copyright 2008-2009, Gerhard Häring.
      Last updated on Jun 24, 2012.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>