.. _dbapinotes:

DBAPI notes
***********

.. currentmodule:: apsw

DBAPI is defined in :pep:`249`. This section describes how APSW complies or differs from it.

Module Interface
================

There is no connect method. Use the :class:`Connection` constructor instead.

The Connection object and any cursors can be used in any thread.  You
cannot use the cursor concurrently in multiple threads for example
calling :meth:`Cursor.execute` at the same time.  If you attempt to do
so then an :exc:`exception <ThreadingViolationError>` will be raised.
The Python Global Interpreter Lock (GIL) is released during all SQLite
API calls allowing for maximum concurrency.

Three different paramstyles are supported. Note that SQLite starts
parameter numbers from one not zero when using *qmark/numeric* style.

.. list-table::
   :header-rows: 1
   :widths: auto

   * - style
     - use
   * - qmark
     - :code:`... WHERE name=?`
   * - numeric
     - :code:`... WHERE name=?4`
   * - named
     - :code:`... WHERE name=:name` or
       :code:`... WHERE name=$name` or
       :code:`... WHERE name=@name`

The DBAPI exceptions are not used.  The :ref:`exceptions <exceptions>`
used correspond to specific SQLite error codes.

Connection Objects
==================

There are no commit or rollback methods.  You can issue queries as
`BEGIN`, `COMMIT`, and `ROLLBACK` to do transactions manually.  You
should use :meth:`with Connection <Connection.__enter__>` to get
automatic transaction control, which includes `nested transactions
<https://sqlite.org/lang_savepoint.html>`__.

Several methods that are defined in DBAPI to be on the cursor are
instead on the Connection object, since this is where SQLite actually
stores the information. Doing operations in any other cursor attached
to the same Connection object does update their values, and this makes
you aware of that.

Cursor Objects
==============

Use :meth:`Cursor.get_description` instead of description. This
information is only obtained on request.

.. _rowcount:

There is no rowcount.  Row counts don't make sense in SQLite any way.
SQLite returns results one row at a time, not calculating the next
result row until you ask for it.  Consequently getting a rowcount
would have to calculate all the result rows and would not reduce the
amount of effort needed.

callproc is not implemented as SQLite doesn't support stored procedures.

:meth:`~Cursor.execute` returns the Cursor object and you can use it
as an iterator to get the results (if any).

:meth:`~Cursor.executemany` returns the Cursor object and you can use
it as an iterator to get the results (if any).

fetchone is not available. Use the cursor as an iterator, or call
Python's `next function
<https://docs.python.org/3/library/functions.html?highlight=next#next>`__
to get the next row.

fetchmany is not available. Use :meth:`~Cursor.fetchall` to get all
remaining results.

nextset is not applicable or implemented.

arraysize is not available as fetchmany isn't.

Neither setinputsizes or setoutputsize are applicable or implemented.

Type objects
============

None of the date or time methods are available since SQLite 3 does not
have a native date or time type.  There are `functions
<https://sqlite.org/lang_datefunc.html>`_ for
manipulating dates and time which are stored as strings or
`Julian days <https://en.wikipedia.org/wiki/Julian_day>`_ (floating
point number).

Use the standard bytes type for blobs.

:doc:`ext` provides functionality to convert and adapt types to those
supported by SQLite.

Optional DB API Extensions
==========================

rownumber is not available.

Exception classes are not available as attributes of Connection but
instead are on the :mod:`apsw` module.  See :ref:`exceptions` for
more details.

Use :attr:`Cursor.connection` to get the associated Connection
object from a cursor.

scroll and messages are not available.

The Cursor object supports the iterator protocol and this is the only
way of getting information back.

To get the last inserted row id, call
:meth:`Connection.last_insert_rowid`. That stores the id from the last
insert on any Cursor associated with the the Connection. You can also
add `select last_insert_rowid() <https://sqlite.org/lang_corefunc.html>`_ to the end of your execute
statements::

  for row in db.execute("""BEGIN;
                               INSERT ... ;
                               INSERT ... ;
                               SELECT last_insert_rowid();
                           COMMIT"""):
     lastrowid=row[0]

There is no errorhandler attribute.
