pragma(key, value=SENTINEL, permanent=False)
Parameters
  • key – Setting name.

  • value – New value for the setting (optional).

  • permanent – Apply this pragma whenever a connection is opened.

Execute a PRAGMA query once on the active connection. If a value is not specified, then the current value will be returned.

If permanent is specified, then the PRAGMA query will also be executed whenever a new connection is opened, ensuring it is always in-effect.

Note

By default this only affects the current connection. If the PRAGMA being executed is not persistent, then you must specify permanent=True to ensure the pragma is set on subsequent connections.

cache_size

Get or set the cache_size pragma for the current connection.

foreign_keys

Get or set the foreign_keys pragma for the current connection.

journal_mode

Get or set the journal_mode pragma.

journal_size_limit

Get or set the journal_size_limit pragma.

mmap_size

Get or set the mmap_size pragma for the current connection.

page_size

Get or set the page_size pragma.

read_uncommitted

Get or set the read_uncommitted pragma for the current connection.

synchronous

Get or set the synchronous pragma for the current connection.

wal_autocheckpoint

Get or set the wal_autocheckpoint pragma for the current connection.

timeout

Get or set the busy timeout (seconds).

register_aggregate(klass, name=None, num_params=-1)
Parameters
  • klass – Class implementing aggregate API.

  • name (str) – Aggregate function name (defaults to name of class).

  • num_params (int) – Number of parameters the aggregate accepts, or -1 for any number.

Register a user-defined aggregate function.

The function will be registered each time a new connection is opened. Additionally, if a connection is already open, the aggregate will be registered with the open connection.

aggregate(name=None, num_params=-1)
Parameters
  • name (str) – Name of the aggregate (defaults to class name).

  • num_params (int) – Number of parameters the aggregate accepts, or -1 for any number.

Class decorator to register a user-defined aggregate function.

Example:

@db.aggregate('md5')
class MD5(object):
    def initialize(self):
        self.md5 = hashlib.md5()

    def step(self, value):
        self.md5.update(value)

    def finalize(self):
        return self.md5.hexdigest()


@db.aggregate()
class Product(object):
    '''Like SUM() except calculates cumulative product.'''
    def __init__(self):
        self.product = 1

    def step(self, value):
        self.product *= value

    def finalize(self):
        return self.product
register_collation(fn, name=None)
Parameters
  • fn – The collation function.

  • name (str) – Name of collation (defaults to function name)

Register a user-defined collation. The collation will be registered each time a new connection is opened. Additionally, if a connection is already open, the collation will be registered with the open connection.

collation(name=None)
Parameters

name (str) – Name of collation (defaults to function name)

Decorator to register a user-defined collation.

Example:

@db.collation('reverse')
def collate_reverse(s1, s2):
    return -cmp(s1, s2)

# Usage:
Book.select().order_by(collate_reverse.collation(Book.title))

# Equivalent:
Book.select().order_by(Book.title.asc(collation='reverse'))

As you might have noticed, the original collate_reverse function has a special attribute called collation attached to it. This extra attribute provides a shorthand way to generate the SQL necessary to use our custom collation.

register_function(fn, name=None, num_params=-1, deterministic=None)
Parameters
  • fn – The user-defined scalar function.

  • name (str) – Name of function (defaults to function name)

  • num_params (int) – Number of arguments the function accepts, or -1 for any number.

  • deterministic (bool) – Whether the function is deterministic for a given input (this is required to use the function in an index). Requires Sqlite 3.20 or newer, and sqlite3 driver support (added to stdlib in Python 3.8).

Register a user-defined scalar function. The function will be registered each time a new connection is opened. Additionally, if a connection is already open, the function will be registered with the open connection.

func(name=None, num_params=-1, deterministic=None)
Parameters
  • name (str) – Name of the function (defaults to function name).

  • num_params (int) – Number of parameters the function accepts, or -1 for any number.

  • deterministic (bool) – Whether the function is deterministic for a given input (this is required to use the function in an index). Requires Sqlite 3.20 or newer, and sqlite3 driver support (added to stdlib in Python 3.8).

Decorator to register a user-defined scalar function.

Example:

@db.func('title_case')
def title_case(s):
    return s.title() if s else ''

# Usage:
title_case_books = Book.select(fn.title_case(Book.title))
register_window_function(klass, name=None, num_params=-1)
Parameters
  • klass – Class implementing window function API.

  • name (str) – Window function name (defaults to name of class).

  • num_params (int) – Number of parameters the function accepts, or -1 for any number.

Register a user-defined window function.

Attention

This feature requires SQLite >= 3.25.0.

The window function will be registered each time a new connection is opened. Additionally, if a connection is already open, the window function will be registered with the open connection.

window_function(name=None, num_params=-1)
Parameters
  • name (str) – Name of the window function (defaults to class name).

  • num_params (int) – Number of parameters the function accepts, or -1 for any number.

Class decorator to register a user-defined window function. Window functions must define the following methods:

  • step(<params>) - receive values from a row and update state.

  • inverse(<params>) - inverse of step() for the given values.

  • value() - return the current value of the window function.

  • finalize() - return the final value of the window function.

Example:

@db.window_function('my_sum')
class MySum(object):
    def __init__(self):
        self._value = 0

    def step(self, value):
        self._value += value

    def inverse(self, value):
        self._value -= value

    def value(self):
        return self._value

    def finalize(self):
        return self._value
unregister_aggregate(name)
Parameters

name – Name of the user-defined aggregate function.

Unregister the user-defined aggregate function.

unregister_collation(name)
Parameters

name – Name of the user-defined collation.

Unregister the user-defined collation.

unregister_function(name)
Parameters

name – Name of the user-defined scalar function.

Unregister the user-defined scalar function.

load_extension(extension_module)

Load the given C extension. If a connection is currently open in the calling thread, then the extension will be loaded for that connection as well as all subsequent connections.

For example, if you’ve compiled the closure table extension and wish to use it in your application, you might write:

db = SqliteExtDatabase('my_app.db')
db.load_extension('closure')
attach(filename, name)
Parameters
  • filename (str) – Database to attach (or :memory: for in-memory)

  • name (str) – Schema name for attached database.

Returns

boolean indicating success

Register another database file that will be attached to every database connection. If the main database is currently connected, the new database will be attached on the open connection.

Note

Databases that are attached using this method will be attached every time a database connection is opened.

detach(name)
Parameters

name (str) – Schema name for attached database.

Returns

boolean indicating success

Unregister another database file that was attached previously with a call to attach(). If the main database is currently connected, the attached database will be detached from the open connection.

atomic(lock_type=None)
Parameters

lock_type (str) – Locking strategy: DEFERRED, IMMEDIATE, EXCLUSIVE.

Create an atomic context-manager, optionally using the specified locking strategy (if unspecified, DEFERRED is used).

Note

Lock type only applies to the outermost atomic() block.

transaction(lock_type=None)
Parameters

lock_type (str) – Locking strategy: DEFERRED, IMMEDIATE, EXCLUSIVE.

Create a transaction context-manager using the specified locking strategy (defaults to DEFERRED).