sqlite_connection
Opens every in-scope SQLite file, as plaintext or SQLCipher by its header.
A file's first 16 bytes decide how it opens, never a setting:
- SQLite's own header,
SQLite format 3and a NUL byte, is plaintext, opened with the stdlibsqlite3. - A missing or empty file is new. It is created encrypted only when cache encryption is on and a usable cache key is found; otherwise plaintext.
- Anything else is SQLCipher, opened with
sqlcipher3under the cache key.
On every SQLCipher connection the key is the very first statement, before any other pragma and before SQLAlchemy's first-connect queries. Cipher settings are SQLCipher 4's defaults, so DB Browser for SQLite opens the files with its own.
A file that can't be opened with the key found raises an
EncryptedStoreKeyError, and is left untouched. :memory: databases never come
through here.
Module
Functions
connect
def connect( path: str | Path, *, read_only: bool = False, **kwargs: Any,) ‑> SqliteConnection:Opens path as plaintext or SQLCipher, whichever its header says.
Arguments
path: The SQLite file. Never":memory:".read_only: Open through a read-onlyfile:URI. A missing file is then an error, as withsqlite3, rather than a new store.**kwargs: Passed to the chosen module'sconnect.
Returns
A stdlib sqlite3 connection for a plaintext file, or a sqlcipher3
connection, already keyed, for an encrypted one. Catch errors from it
with SQLITE_ERRORS.
Raises
ValueError: Ifpathis":memory:".EncryptedStoreNoKeyError: If the file is encrypted and no key was found.EncryptedStoreWrongKeyError: If the key found doesn't open the file.
create_engine_for
def create_engine_for( path: str | Path, **engine_kwargs: Any,) ‑> sqlalchemy.engine.base.Engine:Creates a SQLAlchemy engine for path, plaintext or SQLCipher.
The format is decided once, here, and kept for the engine's life. Every
pooled connection is opened by a creator that keys it first, with
check_same_thread=False.
Arguments
path: The SQLite file. Never":memory:".**engine_kwargs: Passed tocreate_engine, e.g.echo; anyconnect_argsgo to every connection the creator opens.
Returns The engine. SQLAlchemy wraps either module's errors in its own.
Raises
ValueError: Ifpathis":memory:".EncryptedStoreNoKeyError: If the file is encrypted and no key was found.EncryptedStoreWrongKeyError: If the key found doesn't match the key id recorded beside the caches.
detect_format
def detect_format(path: str | Path) ‑> StoreFormat:Reads the first 16 bytes of path to tell what it is.
Only the main file is read, never its -wal.
Arguments
path: The SQLite file.
Returns
NEW for a missing or empty file, PLAINTEXT for one starting with
SQLite's header (or with zeros, a damaged file stdlib sqlite3 then
reports), and ENCRYPTED for anything else.
new_stores_are_encrypted
def new_stores_are_encrypted() ‑> bool:Whether a store created now would be encrypted.
For a store that keeps its earlier, non-SQLite format until it can be
created encrypted. A store opened through connect or create_engine_for
needs no such check.
Returns True when cache encryption is on and a usable cache key is found whose id matches the one recorded beside the caches, if any.
quiet_pandas_dbapi_warning
def quiet_pandas_dbapi_warning(module_name: str) ‑> None:Hides pandas' warning about a SQLCipher connection, for calls from a module.
pandas warns for every DBAPI connection that isn't a stdlib sqlite3 one,
though it reads and writes a sqlcipher3 connection exactly as it does a
sqlite3 one: the two share their driver. Call it just before handing
pandas a connection from connect. The filter is added only when missing,
since warnings.catch_warnings elsewhere may have dropped it, so the
filters change once rather than on every call: unlike a catch_warnings
block, that is thread-safe and leaves the record of other warnings already
shown alone.
Arguments
module_name: The calling module's__name__; pandas attributes the warning to it.
Classes
StoreFormat
class StoreFormat(*args, **kwds):What a SQLite file is, judged by its header.