DBAPI notes¶
DBAPI is defined in PEP 249. This section desribes how APSW complies or differs from it.
Module Interface¶
There is no connect method. Use the Connection
constructor instead.
The Connection object and any cursors can be used in any thread. As
an extreme example, you could call Cursor.next()
in seperate
threads each thread getting the next row. You cannot use the cursor
concurrently in multiple threads for example calling
Cursor.execute()
at the same time. If you attempt to do so then
an exception
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.
qmark | ... WHERE name=? |
numeric | ... WHERE name=?4 |
named | ... WHERE name=:name or... WHERE name=$name |
The DBAPI exceptions are not used. The exceptions used correspond to specific SQLite error codes.
Connection Objects¶
There are no commit or rollback methods. You should use
Cursor.execute()
with BEGIN and COMMIT or ROLLBACK as
appropriate. The SQLite documentation has more details. In
particular note that SQLite does not support nested transactions. You
can only start one transaction and will get an error if you try to
start another one.
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 Cursor.getdescription()
instead of description. This
information is only obtained on request.
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.
execute()
returns the Cursor object and you can use it
as an iterator to get the results (if any).
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
next()
to get the next row, or raises StopIteration when
there are no more results.
fetchmany is not available. Simply use the cursor as an iterator or
call next()
for however many results you want.
fetchall is available, but not too useful. Simply use the cursor as an
iterator, call next()
, or use list which is less typing:
all=list(cursor.execute("...."))
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 for manipulating dates and time which are represented as strings or Julian days (floating point number).
Use the standard Python buffer class for BLOBs in Python 2 and the bytes type in Python 3.
Optional DB API Extensions¶
rownumber is not available.
Exception classes are not available as attributes of Connection but
instead are on the apsw
module. See Exceptions for
more details.
Use Cursor.getconnection()
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
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() to the end of your execute
statements:
for row in cursor.execute("BEGIN; INSERT ... ; INSERT ... ; SELECT last_insert_rowid(); COMMIT"):
lastrowid=row[0]
There is no errorhandler attribute.