The sqlite3 standard module and APSW approached the problem of providing access to SQLite from Python from fundamentally different directions.
APSW provides access in whatever way is normal for SQLite. It makes no effort to hide how SQLite is different from other databases.
sqlite3 tries to provide a DBAPI compliant wrapper for SQLite and in doing so needs to make it have the same behaviour as other databases. Consequently it does hide some of SQLite’s nuances.
I suggest using APSW when you want to directly use SQLite and its functionality or are using your own code to deal with database independence rather than DBAPI. Use sqlite3 and DBAPI if your needs are simple, and you don’t want to use SQLite features.
What APSW does better¶
APSW has the following enhancements/differences over the sqlite3 module:
APSW stays up to date with SQLite. As features are added and functionality changed in SQLite, APSW tracks them.
You can use the same
Connectionacross threads with APSW without needing any additional level of locking. sqlite3 requires that the
cursorsare used in the same thread. You can disable its checking, but unless you are very careful with your own mutexes you will have a crash or a deadlock.
APSW is a single file for the extension,
apsw.pydon Windows and
apsw.soon Unix/Mac (Note PEP 3149). There are no other files needed and the build instructions show you how to include SQLite statically in this file. You can put this file anywhere your Python session can reach.
Nothing happens behind your back. By default sqlite3 tries to manage transactions (for DBAPI compliance) by parsing your SQL for you, but you can turn it off. This can result in very unexpected behaviour with sqlite3.
You can use semi-colons at the end of commands and you can have multiple commands in the execute string in APSW. There are no restrictions on the type of commands used. For example this will work fine in APSW but is not allowed in sqlite3:
import apsw con=apsw.Connection(":memory:") cur=con.cursor() for row in cur.execute("create table foo(x,y,z);insert into foo values (?,?,?);" "insert into foo values(?,?,?);select * from foo;drop table foo;" "create table bar(x,y);insert into bar values(?,?);" "insert into bar values(?,?);select * from bar;", (1,2,3,4,5,6,7,8,9,10)): print (row)
And the output as you would expect:
(1, 2, 3) (4, 5, 6) (7, 8) (9, 10)
Cursor.executemany()also works with statements that return data such as selects, and you can have multiple statements. sqlite3’s
executescript()method doesn’t allow any form of data being returned (it silently ignores any returned data).
sqlite3 swallows exceptions in your callbacks making it far harder to debug problems. That also prevents you from raising exceptions in your callbacks to be handled in your code that called SQLite. sqlite3 does let you turn on printing of tracebacks but that is a poor substitute.
APSW does the right thing as demonstrated by this example. APSW converts Python errors into SQLite errors, so SQLite is aware errors happened.
def badfunc(t): return 1/0 # sqlite3 import sqlite3 con = sqlite3.connect(":memory:") con.create_function("badfunc", 1, badfunc) cur = con.cursor() cur.execute("select badfunc(3)") # apsw import apsw con = apsw.Connection(":memory:") con.createscalarfunction("badfunc", badfunc, 1) cur = con.cursor() cur.execute("select badfunc(3)")
# sqlite3 Traceback (most recent call last): File "func.py", line 8, in ? cur.execute("select badfunc(3)") sqlite3.OperationalError: user-defined function raised exception # apsw Traceback (most recent call last): File "t.py", line 8, in ? cur.execute("select badfunc(3)") File "apsw.c", line 3660, in resetcursor File "apsw.c", line 1871, in user-defined-scalar-badfunc File "t.py", line 3, in badfunc return 1/0
APSW has significantly enhanced debuggability. More details are available than just what is printed out when exceptions happen like above. See augmented stack traces
APSW has execution and row tracers. sqlite3 has no equivalent to execution tracers and does have data adaptors which aren’t the same thing as a row tracer (for example you can’t skip rows or add a new column to each row returned). sqlite3 does have a row factory but you can easily emulate that with the row tracer and
APSW has an apswtrace utility script that traces execution and results in your code without having to modify it in any way. It also outputs summary reports making it easy to see what your most time consuming queries are, which are most popular etc.
APSW has an exception corresponding to each SQLite error code and provides the extended error code. sqlite3 combines several SQLite error codes into corresponding DBAPI exceptions. This is a good example of the difference in approach of the two wrappers.
The APSW test suite is larger and tests more functionality. Virtually every failure condition is tested including running out of memory, error returns etc. Code coverage by the test suite is 99.6%. sqlite3 is good at 81% for C code although there are several places that coverage can be improved. I haven’t measured code coverage for sqlite3’s Python code. The consequences of this are that APSW catches issues earlier and gives far better diagnostics. As an example try returning an unsupported type from a registered scalar function.
APSW is faster than sqlite3 in my testing. Try the speedtest benchmark.
What sqlite3 does better¶
sqlite3 has an adaptor system that lets you pretend SQLite stores and returns more types than it really supports. Note that the database won’t be useful in a non-sqlite3 context (eg PHP code looking at the same database isn’t going to recognise your Point class). You can implement something similar in APSW by intercepting
Cursor.execute()calls that suitably mangles the bindings going to SQLite and does something similar to the rows the iterator returns.