{"id":3815,"library":"sqlitedict","title":"SqliteDict","description":"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.","status":"active","version":"2.1.0","language":"en","source_language":"en","source_url":"https://github.com/piskvorky/sqlitedict","tags":["sqlite","database","dictionary","persistence","key-value","cache","thread-safe"],"install":[{"cmd":"pip install sqlitedict","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"SqliteDict","correct":"from sqlitedict import SqliteDict"}],"quickstart":{"code":"from sqlitedict import SqliteDict\nimport os\n\ndb_path = 'my_data.sqlite'\n\n# Clean up previous run if exists\nif os.path.exists(db_path):\n    os.remove(db_path)\n\n# Open a new SqliteDict with autocommit enabled (recommended for many small writes)\nwith SqliteDict(db_path, autocommit=True) as db:\n    db['user:1'] = {'name': 'Alice', 'age': 30}\n    db['user:2'] = {'name': 'Bob', 'age': 25}\n    db['settings:theme'] = 'dark'\n    print(f\"Added 3 items. Current length: {len(db)}\")\n\n# Re-open the database (autocommit defaults to False if not specified)\nwith SqliteDict(db_path) as db_read_only:\n    print(f\"Retrieved user:1: {db_read_only['user:1']}\")\n    print(f\"All keys: {list(db_read_only.keys())}\")\n\n    # Manual commit is needed if autocommit is False\n    db_read_only['new_item'] = 'This will NOT be saved without commit()'\n    # db_read_only.commit() # Uncomment to save\n\nprint(\"Demonstrating explicit commit\")\n# Open without autocommit, requiring explicit commit\nwith SqliteDict(db_path, autocommit=False) as db_manual:\n    db_manual['product:101'] = {'name': 'Widget', 'price': 19.99}\n    db_manual.commit() # Explicitly commit changes\n    print(f\"Added product:101. Current length: {len(db_manual)}\")\n\n# Verify the manually committed item\nwith SqliteDict(db_path) as db_verify:\n    print(f\"Retrieved product:101: {db_verify['product:101']}\")\n\n# Clean up the database file\nif os.path.exists(db_path):\n    os.remove(db_path)\n","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.7+ or pin `sqlitedict` to version `1.7.0` for older Python versions.","message":"Version 2.0.0 and above dropped support for Python 2.x and requires Python 3.7 or newer. If you need support for older Python versions, you must use `sqlitedict` version 1.7.0 or earlier.","severity":"breaking","affected_versions":"2.0.0+"},{"fix":"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`.","message":"By default, `autocommit` is `False` for performance reasons. Forgetting to call `db.commit()` after modifications will result in unsaved data. Always ensure you commit changes or initialize with `autocommit=True`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"While `sqlitedict` is marketed as 'multithread-safe', this primarily refers to a workaround for Python's `sqlite3` module's threading limitations, serializing requests internally. SQLite itself operates on a single-writer model. True concurrent *writes* from multiple processes or threads will still be serialized and can lead to performance bottlenecks or locking issues if not carefully managed (e.g., with WAL mode and proper error handling).","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Improperly closing an `SqliteDict` instance (e.g., not calling `db.close()` or not using a `with` statement) can lead to data not being saved (if `autocommit` is `False`) or resource leaks. Earlier versions also had reported `AttributeError` or `TypeError` upon closing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"By default, keys are expected to be strings. If using non-string keys (e.g., integers, tuples) or complex objects as keys, `sqlitedict` will serialize them. If deterministic serialization is critical for unordered containers or custom key types, you might need to provide custom `encode_key` and `decode_key` functions to `SqliteDict`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}