apsw, an advanced sqlite driver

The apsw_ext module contains a database class suitable for use with the apsw sqlite driver.

APSW Project page: https://github.com/rogerbinns/apsw

APSW is a really neat library that provides a thin wrapper on top of SQLite’s C interface, making it possible to use all of SQLite’s advanced features.

Here are just a few reasons to use APSW, taken from the documentation:

  • APSW gives all functionality of SQLite, including virtual tables, virtual file system, blob i/o, backups and file control.
  • Connections can be shared across threads without any additional locking.
  • Transactions are managed explicitly by your code.
  • APSW can handle nested transactions.
  • Unicode is handled correctly.
  • APSW is faster.

For more information on the differences between apsw and pysqlite, check the apsw docs.

How to use the APSWDatabase

  1. from apsw_ext import *
  2. db = APSWDatabase(':memory:')
  3. class BaseModel(Model):
  4. class Meta:
  5. database = db
  6. class SomeModel(BaseModel):
  7. col1 = CharField()
  8. col2 = DateTimeField()

apsw_ext API notes

APSWDatabase extends the SqliteExtDatabase and inherits its advanced features.

class APSWDatabase(database, \*connect_kwargs*)

Parameters:
  • database (string) – filename of sqlite database
  • connect_kwargs – keyword arguments passed to apsw when opening a connection
  • register_module(mod_name, mod_inst)

    Provides a way of globally registering a module. For more information, see the documentation on virtual tables.

    Parameters:
    • mod_name (string) – name to use for module
    • mod_inst (object) – an object implementing the Virtual Table interface
  • unregister_module(mod_name)

    Unregister a module.

    Parameters:mod_name (string) – name to use for module

Note

Be sure to use the Field subclasses defined in the apsw_ext module, as they will properly handle adapting the data types for storage.

For example, instead of using peewee.DateTimeField, be sure you are importing and using playhouse.apsw_ext.DateTimeField.