SqliteDict is a Python library that provides a persistent dictionary interface, backed by sqlite3 and using pickle for serialization. It is designed to be multithread-safe as a workaround for Python's `sqlite3` thread limitations and supports multiple tables within a single database file. It offers a simple, Pythonic dict-like interface to an SQLite database, currently at version 2.1.0, and is actively maintained.
pip install sqlitedictVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create and interact with an SqliteDict, covering both `autocommit=True` and manual `commit()` usage, as well as the recommended `with` statement for proper database closing. It stores dictionary objects that are automatically pickled.
Upgrade your Python environment to 3.7+ or pin `sqlitedict` to version `1.7.0` for older Python versions.
Explicitly call `db.commit()` after a series of modifications, or initialize `SqliteDict` with `autocommit=True`. Using a `with` statement also ensures the database is closed, but doesn't implicitly commit if `autocommit` is `False`.
For high write concurrency, consider alternative databases or ensure proper transaction management, retry mechanisms, and potentially enable SQLite's Write-Ahead Logging (WAL) mode. `sqlitedict` is not designed for concurrent high-volume writes across many distinct processes.
Always use the `with` statement when opening an `SqliteDict` instance: `with SqliteDict('path/to/db') as db: ...`. This ensures the database connection is properly closed.For non-string or complex keys, test thoroughly. If unexpected behavior occurs or custom serialization is desired, provide `encode_key` and `decode_key` functions as parameters to the `SqliteDict` constructor to handle key serialization/deserialization explicitly.
pip install sqlitedict
Ensure that only one process or thread attempts to write to the database simultaneously, increase the `timeout` parameter when creating `SqliteDict`, or use separate database files for highly contended data.
Verify that the database file path is correct, ensure the directory exists, and check that the Python process has appropriate read and write permissions for the specified location.
Either remove the `encode` and `decode` parameters to use `sqlitedict`'s default (pickle-based) serialization, or provide custom `default` and `object_hook` functions to `json.dumps` and `json.loads` respectively to handle the serialization of your specific object types.
Ensure that `SqliteDict` instances are only closed when no further operations are needed, or if an instance must be used again after closing, create a new `SqliteDict` instance pointing to the same database file.
No dependency data recorded yet.