apsw, an advanced sqlite driver

The apsw_ext module contains a database class suitable for use withthe 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’sC 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, virtualfile 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.  
  3. db = APSWDatabase(':memory:')
  4.  
  5. class BaseModel(Model):
  6. class Meta:
  7. database = db
  8.  
  9. class SomeModel(BaseModel):
  10. col1 = CharField()
  11. col2 = DateTimeField()

apsw_ext API notes

APSWDatabase extends the SqliteExtDatabase and inheritsits 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

Parameters:

  1. - **mod_name** (_string_) name to use for module
  2. - **mod_inst** (_object_) an object implementing the [Virtual Table](http://rogerbinns.github.io/apsw/vtable.html#vttable-class) interface
  • unregistermodule(_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_extmodule, as they will properly handle adapting the data types for storage.

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