{"id":5726,"library":"sqltrie","title":"SQLTrie","description":"SQLTrie is a Python library that implements a SQL-based prefix tree (trie) data structure, drawing inspiration from `pygtrie` and `python-diskcache`. It enables efficient storage and retrieval of key-value pairs where keys are sequences (e.g., strings) and operations like prefix-based lookups are optimized. The current version, 0.11.2, was released in February 2025. Given its 'Development Status :: 1 - Planning', the release cadence is irregular and the API is evolving.","status":"active","version":"0.11.2","language":"en","source_language":"en","source_url":"https://github.com/treeverse/sqltrie","tags":["data structure","trie","prefix tree","sqlite","database","key-value store","persistent"],"install":[{"cmd":"pip install sqltrie","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"Based on common Python library patterns for data structures and inspirations like `pygtrie`. Official documentation currently lacks explicit usage examples.","symbol":"Trie","correct":"from sqltrie import Trie"}],"quickstart":{"code":"import os\nfrom sqltrie import Trie\n\ndb_path = 'my_sqltrie.db'\n\n# Ensure a clean slate for the example\nif os.path.exists(db_path):\n    os.remove(db_path)\n\n# Initialize a SQLTrie instance, typically taking a filename for SQLite storage.\n# Using a context manager ensures proper closing and data persistence.\nwith Trie(filename=db_path) as trie:\n    trie[\"apple\"] = 100\n    trie[\"apricot\"] = 200\n    trie[\"banana\"] = 300\n    trie[\"bandana\"] = 400\n    trie[\"applepie\"] = 50\n\n    print(f\"Value for 'apple': {trie['apple']}\")\n    print(f\"'banana' exists: {'banana' in trie}\")\n    print(f\"'grape' exists: {'grape' in trie}\")\n\n    print(\"\\nItems with prefix 'ap':\")\n    for key, value in trie.items(prefix=\"ap\"):\n        print(f\"  {key}: {value}\")\n\n    print(\"\\nAll items:\")\n    for key, value in trie.items():\n        print(f\"  {key}: {value}\")\n\n# Data is persisted after exiting the 'with' block.\n# To verify, load the trie again from the same file.\nwith Trie(filename=db_path) as loaded_trie:\n    print(f\"\\nValue for 'apricot' after reload: {loaded_trie['apricot']}\")\n    print(f\"'applepie' exists after reload: {'applepie' in loaded_trie}\")\n\n# Clean up the database file\nos.remove(db_path)\nprint(f\"\\nCleaned up database file '{db_path}'.\")","lang":"python","description":"This quickstart demonstrates common `SQLTrie` operations, including storing and retrieving values by key, checking key existence, and iterating over items with a given prefix. It highlights the use of a context manager for proper database handling and implicitly showcases data persistence in the SQLite backend."},"warnings":[{"fix":"Regularly monitor the project's GitHub repository for updates, changes, and potential discussions about API stability. Be prepared to adapt your code with each new release.","message":"The library is currently in 'Development Status :: 1 - Planning' (Alpha) as per its PyPI classifier. This indicates a highly unstable API that is subject to frequent and significant breaking changes without prior notice. It is not recommended for production environments.","severity":"breaking","affected_versions":"All versions prior to 1.0.0"},{"fix":"Examine the library's source code on GitHub for detailed understanding of its classes and methods. Refer to the documentation of inspiration libraries like `pygtrie` for general trie interface expectations.","message":"Official documentation, particularly for usage examples and API specifics, is minimal. The 'Usage' section in the PyPI description and GitHub README is marked as 'TODO'. Users will likely need to infer functionality from the source code or common patterns of similar trie implementations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Wrap `Trie` instantiation and operations within a `with` statement (e.g., `with Trie(filename='my_db.db') as trie:`) to ensure resources are properly managed and data is committed to disk.","message":"As `sqltrie` is backed by SQLite, ensuring data persistence requires proper closing of the trie instance. Failing to do so (e.g., due to unhandled exceptions or abrupt program termination outside of a context manager) may result in data loss or corruption. Always use a context manager (`with Trie(...)`) or explicitly call a `close()` method if available.","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"}