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
|
<!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>Python Database API Specification 2.0 — KInterbasDB v3.3.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: '3.3.0',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="top" title="KInterbasDB v3.3.0 documentation" href="index.html" />
<link rel="next" title="Compliance to Python Database API 2.0" href="python-db-api-compliance.html" />
<link rel="prev" title="Quick-start Guide / Tutorial" href="tutorial.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="modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="python-db-api-compliance.html" title="Compliance to Python Database API 2.0"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial.html" title="Quick-start Guide / Tutorial"
accesskey="P">previous</a> |</li>
<li><a href="index.html">KInterbasDB v3.3.0 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="python-database-api-specification-2-0">
<h1>Python Database API Specification 2.0<a class="headerlink" href="#python-database-api-specification-2-0" title="Permalink to this headline">¶</a></h1>
<p>KInterbasDB is the Python Database API 2.0 compliant driver
for Firebird. The <cite>Reference / Usage Guide</cite> is therefore divided
into three parts:</p>
<blockquote>
<ul class="simple">
<li>Python Database API 2.0 specification</li>
<li>KInterbasDB Compliance to Python DB 2.0 API specification.</li>
<li>KInterbasDB features beyond Python DB 2.0 API specification.</li>
</ul>
</blockquote>
<p>If you’re familiar to Python DB 2.0 API specification, you may skip
directly to the next topic.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This is a local copy of the specification. The online source copy is
available at <a class="reference external" href="http://www.python.org/topics/database/DatabaseAPI-2.0.html">http://www.python.org/topics/database/DatabaseAPI-2.0.html</a></p>
</div>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>This API has been defined to encourage similarity between the Python
modules that are used to access databases. By doing this, we hope to
achieve a consistency leading to more easily understood modules, code
that is generally more portable across databases, and a broader reach
of database connectivity from Python.</p>
<p>The interface specification consists of several sections:</p>
<ul class="simple">
<li>Module Interface</li>
<li>Connection Objects</li>
<li>Cursor Objects</li>
<li>Type Objects and Constructors</li>
<li>Implementation Hints</li>
<li>Major Changes from 1.0 to 2.0</li>
</ul>
<p>Comments and questions about this specification may be directed to the
<a class="reference external" href="mailto:db-sig%40python.org">SIG for Database Interfacing with Python</a>.</p>
<p>For more information on database interfacing with Python and available
packages see the <a class="reference external" href="http://www.python.org/topics/database/">Database Topics Guide</a> on <a class="reference external" href="http://www.python.org/">www.python.org</a>.</p>
<p>This document describes the Python Database API Specification 2.0. The
previous <a class="reference external" href="http://www.python.org/topics/database/DatabaseAPI-1.0.html">version 1.0 version</a> is
still available as reference. Package writers are encouraged to use
this version of the specification as basis for new interfaces.</p>
</div>
<div class="section" id="module-interface">
<h2>Module Interface<a class="headerlink" href="#module-interface" title="Permalink to this headline">¶</a></h2>
<p>Access to the database is made available through connection objects.
The module must provide the following constructor for these:</p>
<dl class="function">
<dt id="connect">
<!--[connect]--><tt class="descname">connect</tt><big>(</big><em>parameters...</em><big>)</big><a class="headerlink" href="#connect" title="Permalink to this definition">¶</a></dt>
<dd>Constructor for creating a connection to the database. Returns a Connection Object .
It takes a number of parameters which are database dependent. <a class="footnote-reference" href="#f1" id="id1">[1]</a></dd></dl>
<p>These module globals must be defined:</p>
<dl class="data">
<dt id="apilevel">
<!--[apilevel]--><tt class="descname">apilevel</tt><a class="headerlink" href="#apilevel" title="Permalink to this definition">¶</a></dt>
<dd>String constant stating the supported DB API level.
Currently only the strings <cite>‘1.0’</cite> and <cite>‘2.0’</cite> are allowed. If not
given, a <a class="reference external" href="http://www.python.org/topics/database/DatabaseAPI-1.0.html">Database API 1.0</a> level
interface should be assumed.</dd></dl>
<dl class="data">
<dt id="threadsafety">
<!--[threadsafety]--><tt class="descname">threadsafety</tt><a class="headerlink" href="#threadsafety" title="Permalink to this definition">¶</a></dt>
<dd><p>Integer constant stating the level of thread safety the interface supports.
Possible values are:</p>
<blockquote>
<blockquote>
<ul class="simple">
<li><cite>0</cite> = Threads may not share the module.</li>
<li><cite>1</cite> = Threads may share the module, but not connections.</li>
<li><cite>2</cite> = Threads may share the module and connections.</li>
<li><cite>3</cite> = Threads may share the module, connections and cursors. Sharing in the
above context means that two threads may use a resource without
wrapping it using a mutex semaphore to implement resource locking.</li>
</ul>
</blockquote>
<p>Note that you cannot always make external resources thread safe by
managing access using a mutex: the resource may rely on global
variables or other external sources that are beyond your control.</p>
</blockquote>
</dd></dl>
<dl class="data">
<dt id="paramstyle">
<!--[paramstyle]--><tt class="descname">paramstyle</tt><a class="headerlink" href="#paramstyle" title="Permalink to this definition">¶</a></dt>
<dd><p>String constant stating the type of parameter marker
formatting expected by the interface. Possible values are <a class="footnote-reference" href="#f2" id="id2">[2]</a>:</p>
<ul class="simple">
<li><cite>‘qmark’</cite> = Question mark style, e.g. ‘...WHERE name=?’</li>
<li><cite>‘numeric’</cite> = Numeric, positional style, e.g. ‘...WHERE name=:1’</li>
<li><cite>‘named’</cite> = Named style, e.g. ‘...WHERE name=:name’</li>
<li><cite>‘format’</cite> = ANSI C printf format codes, e.g. ‘...WHERE name=%s’</li>
<li><cite>‘pyformat’</cite> = Python extended format codes, e.g. ‘...WHERE name=%(name)s’</li>
</ul>
</dd></dl>
<p>The module should make all error information available through these
exceptions or subclasses thereof:</p>
<dl class="exception">
<dt id="Warning">
<!--[Warning]-->exception <tt class="descname">Warning</tt><a class="headerlink" href="#Warning" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for important warnings like data truncations while inserting,
etc. It must be a subclass of the Python StandardError (defined in the module
exceptions).</dd></dl>
<dl class="exception">
<dt id="Error">
<!--[Error]-->exception <tt class="descname">Error</tt><a class="headerlink" href="#Error" title="Permalink to this definition">¶</a></dt>
<dd>Exception that is the base class of all other error exceptions. You can use this
to catch all errors with one single ‘except’ statement. Warnings are not considered
errors and thus should not use this class as base. It must be a subclass of
the Python StandardError (defined in the module exceptions).</dd></dl>
<dl class="exception">
<dt id="InterfaceError">
<!--[InterfaceError]-->exception <tt class="descname">InterfaceError</tt><a class="headerlink" href="#InterfaceError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for errors that are related to the database interface rather
than the database itself. It must be a subclass of Error.</dd></dl>
<dl class="exception">
<dt id="DatabaseError">
<!--[DatabaseError]-->exception <tt class="descname">DatabaseError</tt><a class="headerlink" href="#DatabaseError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for errors that are related to the database. It must be a subclass
of Error.</dd></dl>
<dl class="exception">
<dt id="DataError">
<!--[DataError]-->exception <tt class="descname">DataError</tt><a class="headerlink" href="#DataError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for errors that are due to problems with the processed data
like division by zero, numeric value out of range, etc. It must be a subclass
of DatabaseError.</dd></dl>
<dl class="exception">
<dt id="OperationalError">
<!--[OperationalError]-->exception <tt class="descname">OperationalError</tt><a class="headerlink" href="#OperationalError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for errors that are related to the database’s operation and
not necessarily under the control of the programmer, e.g. an unexpected disconnect
occurs, the data source name is not found, a transaction could not be processed,
a memory allocation error occurred during processing, etc. It must be a subclass
of DatabaseError.</dd></dl>
<dl class="exception">
<dt id="IntegrityError">
<!--[IntegrityError]-->exception <tt class="descname">IntegrityError</tt><a class="headerlink" href="#IntegrityError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised when the relational integrity of the database is affected,
e.g. a foreign key check fails. It must be a subclass of DatabaseError.</dd></dl>
<dl class="exception">
<dt id="InternalError">
<!--[InternalError]-->exception <tt class="descname">InternalError</tt><a class="headerlink" href="#InternalError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised when the database encounters an internal error, e.g. the cursor
is not valid anymore, the transaction is out of sync, etc. It must be a subclass
of DatabaseError.</dd></dl>
<dl class="exception">
<dt id="ProgrammingError">
<!--[ProgrammingError]-->exception <tt class="descname">ProgrammingError</tt><a class="headerlink" href="#ProgrammingError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised for programming errors, e.g. table not found or already exists,
syntax error in the SQL statement, wrong number of parameters specified, etc.
It must be a subclass of DatabaseError.</dd></dl>
<dl class="exception">
<dt id="NotSupportedError">
<!--[NotSupportedError]-->exception <tt class="descname">NotSupportedError</tt><a class="headerlink" href="#NotSupportedError" title="Permalink to this definition">¶</a></dt>
<dd>Exception raised in case a method or database API was used which is not supported
by the database, e.g. requesting a .rollback() on a connection that does not support
transaction or has transactions turned off. It must be a subclass of DatabaseError.</dd></dl>
<p>This is the exception inheritance layout:</p>
<div class="highlight-python"><pre> StandardError
|__Warning
|__Error
|__InterfaceError
|__DatabaseError
|__DataError
|__OperationalError
|__IntegrityError
|__InternalError
|__ProgrammingError
|__NotSupportedError
Note: The values of these exceptions are not defined. They should give
the user a fairly good idea of what went wrong though.</pre>
</div>
</div>
<div class="section" id="connection-objects">
<h2>Connection Objects<a class="headerlink" href="#connection-objects" title="Permalink to this headline">¶</a></h2>
<p>Connections Objects should respond to the following methods:</p>
<dl class="class">
<dt id="Connection">
<!--[Connection]-->class <tt class="descname">Connection</tt><a class="headerlink" href="#Connection" title="Permalink to this definition">¶</a></dt>
<dd><dl class="method">
<dt id="Connection.close">
<!--[Connection.close]--><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#Connection.close" title="Permalink to this definition">¶</a></dt>
<dd>Close the connection now (rather than whenever __del__ is called). The connection
will be unusable from this point forward; an <cite>Error</cite> (or subclass) exception will
be raised if any operation is attempted with the connection. The same applies to
all cursor objects trying to use the connection.</dd></dl>
<dl class="method">
<dt id="Connection.commit">
<!--[Connection.commit]--><tt class="descname">commit</tt><big>(</big><big>)</big><a class="headerlink" href="#Connection.commit" title="Permalink to this definition">¶</a></dt>
<dd>Commit any pending transaction to the database. Note
that if the database supports an auto-commit feature, this must be
initially off. An interface method may be provided to turn it back on.
Database modules that do not support transactions should implement
this method with void functionality.</dd></dl>
<dl class="method">
<dt id="Connection.rollback">
<!--[Connection.rollback]--><tt class="descname">rollback</tt><big>(</big><big>)</big><a class="headerlink" href="#Connection.rollback" title="Permalink to this definition">¶</a></dt>
<dd>This method is optional since not all databases
provide transaction support. <a class="footnote-reference" href="#f3" id="id3">[3]</a> In case a database does provide
transactions this method causes the the database to roll back to the
start of any pending transaction. Closing a connection without
committing the changes first will cause an implicit rollback to be
performed.</dd></dl>
<dl class="method">
<dt id="Connection.cursor">
<!--[Connection.cursor]--><tt class="descname">cursor</tt><big>(</big><big>)</big><a class="headerlink" href="#Connection.cursor" title="Permalink to this definition">¶</a></dt>
<dd>Return a new Cursor Object using the connection. If
the database does not provide a direct cursor concept, the module will
have to emulate cursors using other means to the extent needed by this
specification. <a class="footnote-reference" href="#f4" id="id4">[4]</a></dd></dl>
</dd></dl>
</div>
<div class="section" id="cursor-objects">
<h2>Cursor Objects<a class="headerlink" href="#cursor-objects" title="Permalink to this headline">¶</a></h2>
<p>These objects represent a database cursor, which is used to manage the
context of a fetch operation. Cursor Objects should respond to the
following methods and attributes:</p>
<dl class="class">
<dt id="Cursor">
<!--[Cursor]-->class <tt class="descname">Cursor</tt><a class="headerlink" href="#Cursor" title="Permalink to this definition">¶</a></dt>
<dd><dl class="attribute">
<dt id="Cursor.description">
<!--[Cursor.description]--><tt class="descname">description</tt><a class="headerlink" href="#Cursor.description" title="Permalink to this definition">¶</a></dt>
<dd>This read-only attribute is a sequence of 7-item
sequences. Each of these sequences contains information describing one
result column: <cite>(name, type_code, display_size, internal_size,
precision, scale, null_ok)</cite>. This attribute will be <cite>None</cite> for
operations that do not return rows or if the cursor has not had an
operation invoked via the <cite>executeXXX()</cite> method yet. The <cite>type_code</cite>
can be interpreted by comparing it to the Type Objects specified in
the section below.</dd></dl>
<dl class="attribute">
<dt id="Cursor.rowcount">
<!--[Cursor.rowcount]--><tt class="descname">rowcount</tt><a class="headerlink" href="#Cursor.rowcount" title="Permalink to this definition">¶</a></dt>
<dd>This read-only attribute specifies the number of rows
that the last <cite>executeXXX()</cite> produced (for DQL statements like select)
or affected (for DML statements like update or insert ). The
attribute is -1 in case no <cite>executeXXX()</cite> has been performed on the
cursor or the rowcount of the last operation is not determinable by
the interface. <a class="footnote-reference" href="#f7" id="id5">[7]</a></dd></dl>
<dl class="method">
<dt id="Cursor.callproc">
<!--[Cursor.callproc]--><tt class="descname">callproc</tt><big>(</big><em>procname</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#Cursor.callproc" title="Permalink to this definition">¶</a></dt>
<dd>This method is optional since not all databases provide stored procedures.
<a class="footnote-reference" href="#f3" id="id6">[3]</a> Call a stored database procedure with the given name. The sequence of parameters
must contain one entry for each argument that the procedure expects.
The result of the call is returned as modified copy of the input
sequence. Input parameters are left untouched, output and input/output
parameters replaced with possibly new values. The procedure may also
provide a result set as output. This must then be made available
through the standard <cite>fetchXXX()</cite> methods.</dd></dl>
<dl class="method">
<dt id="Cursor.close">
<!--[Cursor.close]--><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#Cursor.close" title="Permalink to this definition">¶</a></dt>
<dd>Close the cursor now (rather than whenever __del__ is
called). The cursor will be unusable from this point forward; an
<cite>Error</cite> (or subclass) exception will be raised if any operation is
attempted with the cursor.</dd></dl>
<dl class="method">
<dt id="Cursor.execute">
<!--[Cursor.execute]--><tt class="descname">execute</tt><big>(</big><em>operation</em><span class="optional">[</span>, <em>parameters</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#Cursor.execute" title="Permalink to this definition">¶</a></dt>
<dd>Prepare and execute a database operation (query or command).
Parameters may be provided as sequence or mapping and will be bound to
variables in the operation. Variables are specified in a database-specific
notation (see the module’s <cite>paramstyle</cite> attribute for details).
<a class="footnote-reference" href="#f5" id="id7">[5]</a> A reference to the operation will be retained by the cursor. If the same
operation object is passed in again, then the cursor can optimize its behavior.
This is most effective for algorithms where the same operation is used, but
different parameters are bound to it (many times). For maximum
efficiency when reusing an operation, it is best to use the
setinputsizes() method to specify the parameter types and sizes ahead
of time. It is legal for a parameter to not match the predefined
information; the implementation should compensate, possibly with a
loss of efficiency. The parameters may also be specified as list of
tuples to e.g. insert multiple rows in a single operation, but this
kind of usage is depreciated: <cite>executemany()</cite> should be used instead.
Return values are not defined.</dd></dl>
<dl class="method">
<dt id="Cursor.executemany">
<!--[Cursor.executemany]--><tt class="descname">executemany</tt><big>(</big><em>operation</em>, <em>seq_of_parameters</em><big>)</big><a class="headerlink" href="#Cursor.executemany" title="Permalink to this definition">¶</a></dt>
<dd>Prepare a database operation (query or command) and then execute it against all parameter
sequences or mappings found in the sequence <cite>seq_of_parameters</cite>.
Modules are free to implement this method using multiple calls to the
<cite>execute()</cite> method or by using array operations to have the database
process the sequence as a whole in one call. The same comments as for
<cite>execute()</cite> also apply accordingly to this method. Return values are
not defined.</dd></dl>
<dl class="method">
<dt id="Cursor.fetchone">
<!--[Cursor.fetchone]--><tt class="descname">fetchone</tt><big>(</big><big>)</big><a class="headerlink" href="#Cursor.fetchone" title="Permalink to this definition">¶</a></dt>
<dd>Fetch the next row of a query result set, returning
a single sequence, or <cite>None</cite> when no more data is available. <a class="footnote-reference" href="#f6" id="id8">[6]</a> An
<cite>Error</cite> (or subclass) exception is raised if the previous call to
<cite>executeXXX()</cite> did not produce any result set or no call was issued yet.</dd></dl>
<dl class="method">
<dt id="Cursor.fetchmany">
<!--[Cursor.fetchmany]--><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="#Cursor.fetchmany" title="Permalink to this definition">¶</a></dt>
<dd>Fetch the next set of rows of a query result, returning a sequence of sequences
(e.g. a list of tuples). An empty sequence is returned when no more rows are
available. The number of rows to fetch per call is specified by the
parameter. If it is not given, the cursor’s <cite>arraysize</cite> 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. An <cite>Error</cite> (or subclass) exception is raised if the previous
call to <cite>executeXXX()</cite> did not produce any result set or no call was
issued yet. Note there are performance considerations involved with
the size parameter. For optimal performance, it is usually best to use
the arraysize attribute. If the size parameter is used, then it is
best for it to retain the same value from one <cite>fetchmany()</cite> call to the next.</dd></dl>
<dl class="method">
<dt id="Cursor.fetchall">
<!--[Cursor.fetchall]--><tt class="descname">fetchall</tt><big>(</big><big>)</big><a class="headerlink" href="#Cursor.fetchall" title="Permalink to this definition">¶</a></dt>
<dd>Fetch all (remaining) rows of a query result, returning them as a sequence
of sequences (e.g. a list of tuples).
Note that the cursor’s <cite>arraysize</cite> attribute can affect the
performance of this operation. An <cite>Error</cite> (or subclass) exception is
raised if the previous call to <cite>executeXXX()</cite> did not produce any
result set or no call was issued yet.</dd></dl>
<dl class="method">
<dt id="Cursor.nextset">
<!--[Cursor.nextset]--><tt class="descname">nextset</tt><big>(</big><big>)</big><a class="headerlink" href="#Cursor.nextset" title="Permalink to this definition">¶</a></dt>
<dd>This method is optional since not all databases
support multiple result sets. <a class="footnote-reference" href="#f3" id="id9">[3]</a> This method will make the cursor
skip to the next available set, discarding any remaining rows from the
current set. If there are no more sets, the method returns <cite>None</cite>.
Otherwise, it returns a true value and subsequent calls to the fetch
methods will return rows from the next result set. An <cite>Error</cite> (or
subclass) exception is raised if the previous call to <cite>executeXXX()</cite>
did not produce any result set or no call was issued yet.</dd></dl>
<dl class="method">
<dt id="Cursor.setinputsizes">
<!--[Cursor.setinputsizes]--><tt class="descname">setinputsizes</tt><big>(</big><em>sizes</em><big>)</big><a class="headerlink" href="#Cursor.setinputsizes" title="Permalink to this definition">¶</a></dt>
<dd>This can be used before a call to <cite>executeXXX()</cite> to predefine memory areas
for the operation’s parameters. <cite>sizes</cite> is specified as a sequence – one
item for each input parameter. The item should be a Type Object that corresponds
to the input that will be used, or it should be an integer specifying the
maximum length of a string parameter. If the item is <cite>None</cite>, then no
predefined memory area will be reserved for that column (this is
useful to avoid predefined areas for large inputs). This method would
be used before the <cite>executeXXX()</cite> method is invoked. Implementations
are free to have this method do nothing and users are free to not use it.</dd></dl>
<dl class="method">
<dt id="Cursor.setoutputsize">
<!--[Cursor.setoutputsize]--><tt class="descname">setoutputsize</tt><big>(</big><em>size</em><span class="optional">[</span>, <em>column</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#Cursor.setoutputsize" title="Permalink to this definition">¶</a></dt>
<dd>Set a column buffer size for fetches of large columns (e.g. LONGs, BLOBs, etc.).
The column is specified as an index into the result sequence. Not specifying the
column will set the default size for all large columns in the cursor.
This method would be used before the <cite>executeXXX()</cite> method is invoked.
Implementations are free to have this method do nothing and users are
free to not use it.</dd></dl>
</dd></dl>
</div>
<div class="section" id="type-objects-and-constructors">
<h2>Type Objects and Constructors<a class="headerlink" href="#type-objects-and-constructors" title="Permalink to this headline">¶</a></h2>
<p>Many databases need to have the input in a particular format for
binding to an operation’s input parameters. For example, if an input
is destined for a DATE column, then it must be bound to the database
in a particular string format. Similar problems exist for “Row ID”
columns or large binary items (e.g. blobs or RAW columns). This
presents problems for Python since the parameters to the
<cite>executeXXX()</cite> method are untyped. When the database module sees a
Python string object, it doesn’t know if it should be bound as a
simple CHAR column, as a raw BINARY item, or as a DATE. To overcome
this problem, a module must provide the constructors defined below to
create objects that can hold special values. When passed to the cursor
methods, the module can then detect the proper type of the input
parameter and bind it accordingly. A Cursor Object’s <cite>description</cite>
attribute returns information about each of the result columns of a
query. The <cite>type_code</cite> must compare equal to one of Type Objects
defined below. Type Objects may be equal to more than one type code
(e.g. DATETIME could be equal to the type codes for date, time and
timestamp columns; see the Implementation Hints below for details).
The module exports the following constructors and singletons:</p>
<dl class="function">
<dt id="Date">
<!--[Date]--><tt class="descname">Date</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em><big>)</big><a class="headerlink" href="#Date" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a date value.</dd></dl>
<dl class="function">
<dt id="Time">
<!--[Time]--><tt class="descname">Time</tt><big>(</big><em>hour</em>, <em>minute</em>, <em>second</em><big>)</big><a class="headerlink" href="#Time" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a time value.</dd></dl>
<dl class="function">
<dt id="Timestamp">
<!--[Timestamp]--><tt class="descname">Timestamp</tt><big>(</big><em>year</em>, <em>month</em>, <em>day</em>, <em>hour</em>, <em>minute</em>, <em>second</em><big>)</big><a class="headerlink" href="#Timestamp" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a time stamp value.</dd></dl>
<dl class="function">
<dt id="DateFromTicks">
<!--[DateFromTicks]--><tt class="descname">DateFromTicks</tt><big>(</big><em>ticks</em><big>)</big><a class="headerlink" href="#DateFromTicks" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a date value from the given
ticks value (number of seconds since the epoch; see the documentation
of the standard Python time module for details).</dd></dl>
<dl class="function">
<dt id="TimeFromTicks">
<!--[TimeFromTicks]--><tt class="descname">TimeFromTicks</tt><big>(</big><em>ticks</em><big>)</big><a class="headerlink" href="#TimeFromTicks" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a time value from the given
ticks value (number of seconds since the epoch; see the documentation
of the standard Python time module for details).</dd></dl>
<dl class="function">
<dt id="TimestampFromTicks">
<!--[TimestampFromTicks]--><tt class="descname">TimestampFromTicks</tt><big>(</big><em>ticks</em><big>)</big><a class="headerlink" href="#TimestampFromTicks" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object holding a time stamp value from the given
ticks value (number of seconds since the epoch; see the documentation
of the standard Python time module for details).</dd></dl>
<dl class="function">
<dt id="Binary">
<!--[Binary]--><tt class="descname">Binary</tt><big>(</big><em>string</em><big>)</big><a class="headerlink" href="#Binary" title="Permalink to this definition">¶</a></dt>
<dd>This function constructs an object capable of holding a binary (long) string value.</dd></dl>
<dl class="data">
<dt id="STRING">
<!--[STRING]--><tt class="descname">STRING</tt><a class="headerlink" href="#STRING" title="Permalink to this definition">¶</a></dt>
<dd>This type object is used to describe columns in a database that are string-based
(e.g. CHAR).</dd></dl>
<dl class="data">
<dt id="BINARY">
<!--[BINARY]--><tt class="descname">BINARY</tt><a class="headerlink" href="#BINARY" title="Permalink to this definition">¶</a></dt>
<dd>This type object is used to describe (long) binary columns in a database
(e.g. LONG, RAW, BLOBs).</dd></dl>
<dl class="data">
<dt id="NUMBER">
<!--[NUMBER]--><tt class="descname">NUMBER</tt><a class="headerlink" href="#NUMBER" title="Permalink to this definition">¶</a></dt>
<dd>This type object is used to describe numeric columns in a database.</dd></dl>
<dl class="data">
<dt id="DATETIME">
<!--[DATETIME]--><tt class="descname">DATETIME</tt><a class="headerlink" href="#DATETIME" title="Permalink to this definition">¶</a></dt>
<dd>This type object is used to describe date/time columns in a database.</dd></dl>
<dl class="data">
<dt id="ROWID">
<!--[ROWID]--><tt class="descname">ROWID</tt><a class="headerlink" href="#ROWID" title="Permalink to this definition">¶</a></dt>
<dd>This type object is used to describe the “Row ID” column in a database.</dd></dl>
<p>SQL NULL values are represented by the Python <cite>None</cite> singleton on
input and output. Note: Usage of Unix ticks for database interfacing
can cause troubles because of the limited date range they cover.</p>
</div>
<div class="section" id="implementation-hints">
<h2>Implementation Hints<a class="headerlink" href="#implementation-hints" title="Permalink to this headline">¶</a></h2>
<ul class="simple">
<li>The preferred object types for the date/time objects are those
defined in the <a class="reference external" href="http://starship.python.net/%7Elemburg/mxDateTime.html">mxDateTime</a> package. It
provides all necessary constructors and methods both at Python and C
level.</li>
<li>The preferred object type for Binary objects are the buffer types
available in standard Python starting with version 1.5.2. Please see
the Python documentation for details. For information about the the C
interface have a look at Include/bufferobject.h and
Objects/bufferobject.c in the Python source distribution.</li>
<li>Here is a sample implementation of the Unix ticks based constructors
for date/time delegating work to the generic constructors:</li>
</ul>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">time</span>
<span class="k">def</span> <span class="nf">DateFromTicks</span><span class="p">(</span><span class="n">ticks</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">apply</span><span class="p">(</span><span class="n">Date</span><span class="p">,</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">ticks</span><span class="p">)[:</span><span class="mf">3</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">TimeFromTicks</span><span class="p">(</span><span class="n">ticks</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">apply</span><span class="p">(</span><span class="n">Time</span><span class="p">,</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">ticks</span><span class="p">)[</span><span class="mf">3</span><span class="p">:</span><span class="mf">6</span><span class="p">])</span>
<span class="k">def</span> <span class="nf">TimestampFromTicks</span><span class="p">(</span><span class="n">ticks</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">apply</span><span class="p">(</span><span class="n">Timestamp</span><span class="p">,</span><span class="n">time</span><span class="o">.</span><span class="n">localtime</span><span class="p">(</span><span class="n">ticks</span><span class="p">)[:</span><span class="mf">6</span><span class="p">])</span>
</pre></div>
</div>
<ul class="simple">
<li>This Python class allows implementing the above type objects even
though the description type code field yields multiple values for on
type object:</li>
</ul>
<div class="highlight-python"><pre> class DBAPITypeObject:
def __init__(self,*values):
self.values = values
def __cmp__(self,other):
if other in self.values:
return 0
if other < self.values:
return 1
else:
return -1
The resulting type object compares equal to all values passed to the
constructor.</pre>
</div>
<ul class="simple">
<li>Here is a snippet of Python code that implements the exception
hierarchy defined above:</li>
</ul>
<div class="highlight-python"><pre> import exceptions
class Error(exceptions.StandardError):
pass
class Warning(exceptions.StandardError):
pass
class InterfaceError(Error):
pass
class DatabaseError(Error):
pass
class InternalError(DatabaseError):
pass
class OperationalError(DatabaseError):
pass
class ProgrammingError(DatabaseError):
pass
class IntegrityError(DatabaseError):
pass
class DataError(DatabaseError):
pass
class NotSupportedError(DatabaseError):
pass
In C you can use the `PyErr_NewException(fullname, base, NULL)` API to
create the exception objects.</pre>
</div>
</div>
<div class="section" id="major-changes-from-version-1-0-to-version-2-0">
<h2>Major Changes from Version 1.0 to Version 2.0<a class="headerlink" href="#major-changes-from-version-1-0-to-version-2-0" title="Permalink to this headline">¶</a></h2>
<blockquote>
<p>The Python Database API 2.0 introduces a few major changes compared to
the 1.0 version. Because some of these changes will cause existing <a class="reference external" href="http://www.python.org/topics/database/DatabaseAPI-1.0.html">DB
API 1.0</a> based
scripts to break, the major version number was adjusted to reflect
this change. These are the most important changes from 1.0 to 2.0:</p>
<blockquote>
<ul class="simple">
<li>The need for a separate dbi module was dropped and the functionality
merged into the module interface itself.</li>
<li>New constructors and Type Objects were added for date/time values,
the RAW Type Object was renamed to BINARY. The resulting set should
cover all basic data types commonly found in modern SQL databases.</li>
<li>New constants (apilevel, threadlevel, paramstyle) and methods
(executemany, nextset) were added to provide better database bindings.</li>
<li>The semantics of .callproc() needed to call stored procedures are
now clearly defined.</li>
<li>The definition of the .execute() return value changed. Previously,
the return value was based on the SQL statement type (which was hard
to implement right) – it is undefined now; use the more flexible
.rowcount attribute instead. Modules are free to return the old style
return values, but these are no longer mandated by the specification
and should be considered database interface dependent.</li>
<li>Class based exceptions were incorporated into the specification.
Module implementors are free to extend the exception layout defined in
this specification by subclassing the defined exception classes.</li>
</ul>
</blockquote>
</blockquote>
</div>
<div class="section" id="open-issues">
<h2>Open Issues<a class="headerlink" href="#open-issues" title="Permalink to this headline">¶</a></h2>
<p>Although the version 2.0 specification clarifies a lot of questions
that were left open in the 1.0 version, there are still some remaining
issues:</p>
<ul class="simple">
<li>Define a useful return value for .nextset() for the case where a new
result set is available.</li>
<li>Create a fixed point numeric type for use as loss-less monetary and
decimal interchange format.</li>
</ul>
</div>
<div class="section" id="footnotes">
<h2>Footnotes<a class="headerlink" href="#footnotes" title="Permalink to this headline">¶</a></h2>
<table class="docutils footnote" frame="void" id="f1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>As a guideline the connection constructor parameters should be
implemented as keyword parameters for more intuitive use and follow
this order of parameters: <cite>dsn</cite> = Data source name as string <cite>user</cite> =
User name as string (optional) <cite>password</cite> = Password as string
(optional) <cite>host</cite> = Hostname (optional) <cite>database</cite> = Database name
(optional) E.g. a connect could look like this:
<cite>connect(dsn=’myhost:MYDB’,user=’guido’,password=‘234$?’)</cite></td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id2">[2]</a></td><td>Module implementors should prefer ‘numeric’, ‘named’ or ‘pyformat’
over the other formats because these offer more clarity and
flexibility.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f3" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[3]</td><td><em>(<a class="fn-backref" href="#id3">1</a>, <a class="fn-backref" href="#id6">2</a>, <a class="fn-backref" href="#id9">3</a>)</em> If the database does not support the functionality required by the
method, the interface should throw an exception in case the method is
used. The preferred approach is to not implement the method and thus
have Python generate an <cite>AttributeError</cite> in case the method is
requested. This allows the programmer to check for database
capabilities using the standard <cite>hasattr()</cite> function. For some
dynamically configured interfaces it may not be appropriate to require
dynamically making the method available. These interfaces should then
raise a <cite>NotSupportedError</cite> to indicate the non-ability to perform the
roll back when the method is invoked.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f4" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4">[4]</a></td><td>A database interface may choose to support named cursors by
allowing a string argument to the method. This feature is not part of
the specification, since it complicates semantics of the <cite>.fetchXXX()</cite>
methods.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f5" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id7">[5]</a></td><td>The module will use the __getitem__ method of the parameters object
to map either positions (integers) or names (strings) to parameter
values. This allows for both sequences and mappings to be used as
input. The term “bound” refers to the process of binding an input
value to a database execution buffer. In practical terms, this means
that the input value is directly used as a value in the operation. The
client should not be required to “escape” the value so that it can be
used – the value should be equal to the actual database value.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f6" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id8">[6]</a></td><td>Note that the interface may implement row fetching using arrays and
other optimizations. It is not guaranteed that a call to this method
will only move the associated cursor forward by one row.</td></tr>
</tbody>
</table>
<table class="docutils footnote" frame="void" id="f7" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id5">[7]</a></td><td>The <cite>rowcount</cite> attribute may be coded in a way that updates its
value dynamically. This can be useful for databases that return
useable rowcount values only after the first call to a <cite>.fetchXXX()</cite>
method.</td></tr>
</tbody>
</table>
</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 external" href="">Python Database API Specification 2.0</a><ul>
<li><a class="reference external" href="#introduction">Introduction</a></li>
<li><a class="reference external" href="#module-interface">Module Interface</a></li>
<li><a class="reference external" href="#connection-objects">Connection Objects</a></li>
<li><a class="reference external" href="#cursor-objects">Cursor Objects</a></li>
<li><a class="reference external" href="#type-objects-and-constructors">Type Objects and Constructors</a></li>
<li><a class="reference external" href="#implementation-hints">Implementation Hints</a></li>
<li><a class="reference external" href="#major-changes-from-version-1-0-to-version-2-0">Major Changes from Version 1.0 to Version 2.0</a></li>
<li><a class="reference external" href="#open-issues">Open Issues</a></li>
<li><a class="reference external" href="#footnotes">Footnotes</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="tutorial.html" title="previous chapter">Quick-start Guide / Tutorial</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="python-db-api-compliance.html" title="next chapter">Compliance to Python Database API 2.0</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/Python-DB-API-2.0.txt">Show Source</a></li>
</ul>
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" size="18" /> <input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</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"
accesskey="I">index</a></li>
<li class="right" >
<a href="modindex.html" title="Global Module Index"
accesskey="M">modules</a> |</li>
<li class="right" >
<a href="python-db-api-compliance.html" title="Compliance to Python Database API 2.0"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="tutorial.html" title="Quick-start Guide / Tutorial"
accesskey="P">previous</a> |</li>
<li><a href="index.html">KInterbasDB v3.3.0 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2009, David Rushby, Pavel Cisar.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1.
</div>
</body>
</html>
|