# 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 the recommended group:

    import apsw.bestpractice

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

## API

Ensure SQLite usage prevents common mistakes, and get best performance.

apsw.bestpractice.apply(which)

  Applies library immediately and connection to new connections

  Return type:

    `None`

apsw.bestpractice.connection_busy_timeout(connection, duration_ms=100)

  Sets a short busy timeout

  Parameters:

    duration_ms (`int`) – How many thousandths of a second to wait

  Return type:

    `None`

  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.

apsw.bestpractice.connection_dqs(connection)

  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.

  Return type:

    `None`

apsw.bestpractice.connection_enable_foreign_keys(connection)

  Enables foreign key constraints

  Foreign keys need to be enabled to have an effect.

  Return type:

    `None`

apsw.bestpractice.connection_optimize(connection)

  Enables query planner optimization

  It enables the query planner to record cases when it would benefit from having accurate statistics about tables and indexes for the queries you make.

  You can later run `connection.pragma("optimize")` to have those statistics updated, such as when closing a database or periodically when the database is open for long periods of time. The statistics are recorded in the database and help with future queries during this connection, and all future connections.

  There is more detail in the documentation.

  Return type:

    `None`

apsw.bestpractice.connection_recursive_triggers(connection)

  Recursive triggers are off for historical backwards compatibility

  This enables them.

  Return type:

    `None`

apsw.bestpractice.connection_wal(connection)

  Turns on write ahead logging

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

  Return type:

    `None`

apsw.bestpractice.library_logging()

  Forwards SQLite logging to Python logging module

  See `apsw.ext.log_sqlite()`

  Return type:

    `None`

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

  All of them