# Best Practice

## Explanation

Because SQLite keeps very strong backwards compatibility, there are several quirks and settings improvements that are not automatically done. This module does them for you, and keeps up to date with SQLite best practises. Several are described in the SQLite documentation.

Functions whose name begin with `library` apply to the SQLite library as a whole, while those with `connection` apply to individual connections.

You can call the individual functions, or `apsw.bestpractice.apply()` to apply several. `apply()` will setup `apsw.connection_hooks` to affect future `connections` that are made. `recommended` is recommended:

    import apsw.bestpractice

    apsw.bestpractise.apply(apsw.bestpractice.recommended)

## API

Module to ensure SQLite usage prevents common mistakes, and best performance.

apply(which: tuple[Callable, ...]) → None

  Applies library immediately and connection to new connections

connection_busy_timeout(connection: Connection, duration_ms: int = 100) → None

  Sets a short busy timeout

  Parameters:

    duration_ms -- How many thousandths of a second to wait

  When another thread or process has locked the database, SQLite immediately raises `apsw.BusyError`. Changing the busy timeout gives a grace period during which SQLite retries.

connection_dqs(connection: Connection) → None

  Double quotes are for identifiers only, not strings

  Turns off allowing double quoted strings if they don't match any identifier (column/table names etc), making it an error to use double quotes around strings. SQL strings use single quotes.

connection_enable_foreign_keys(connection: Connection) → None

  Enables foreign key constraints

  Foreign keys need to be enabled to have an effect.

connection_wal(connection: Connection) → None

  Turns on write ahead logging

  Reduces contention and improves write performance. WAL is described here.

library_logging() → None

  Forwards SQLite logging to Python logging module

  See `apsw.ext.log_sqlite()`

recommended: tuple[Callable, ...] = (<function connection_wal>, <function connection_busy_timeout>, <function connection_enable_foreign_keys>, <function connection_dqs>, <function library_logging>)

  All of them