English 中文(简体)
Peewee - SQLite Extensions
  • 时间:2024-09-17

Peewee - SQLite Extensions


Previous Page Next Page  

Peewee comes with a Playhouse namespace. It is a collection of various extension modules. One of them is a playhouse.sqpte_ext module. It mainly defines SqpteExtDatabase class which inherits SqpteDatabase class, supports following additional features −

Features of SQLite Extensions

The features of SQLite Extensions which are supported by Peewee are as follows −

    Full-text search.

    JavaScript Object Notation (JSON) extension integration.

    Closure table extension support.

    LSM1 extension support.

    User-defined table functions.

    Support for onpne backups using backup API: backup_to_file().

    BLOB API support, for efficient binary data storage.

JSON data can be stored, if a special JSONField is declared as one of the field attributes.


class MyModel(Model):
   json_data = JSONField(json_dumps=my_json_dumps)

To activate full-text search, the model can have DocIdField to define primary key.


class NoteIndex(FTSModel):
   docid = DocIDField()
   content = SearchField()

   class Meta:
      database = db

FTSModel is a Subclass of VirtualModel which is available at http://docs.peewee-orm.com/en/latest/peewee/sqpte_ext.html#VirtualModel to be used with the FTS3 and FTS4 full-text search extensions. Sqpte will treat all column types as TEXT (although, you can store other data types, Sqpte will treat them as text).

SearchField is a Field-class to be used for columns on models representing full-text search virtual tables.

SqpteDatabase supports AutoField for increasing primary key. However, SqpteExtDatabase supports AutoIncrementField to ensure that primary always increases monotonically, irrespective of row deletions.

SqpteQ module in playhouse namespace (playhouse.sqpteq) defines subclass of SqpteExeDatabase to handle seriapsed concurrent writes to a SQpte database.

On the other hand, playhouse.apsw module carries support for apsw sqpte driver. Another Python SQLite Wrapper (APSW) is fast and can handle nested transactions, that are managed exppcitly by you code.


from apsw_ext import *
db = APSWDatabase( testdb )

class BaseModel(Model):
   class Meta:
      database = db

class MyModel(BaseModel):
   field1 = CharField()
   field2 = DateTimeField()
Advertisements