{"id":15564037,"url":"https://github.com/eigenein/sqlitemap","last_synced_at":"2025-07-10T05:36:44.379Z","repository":{"id":57470592,"uuid":"178052917","full_name":"eigenein/sqlitemap","owner":"eigenein","description":"Dictionary interface to an SQLite database","archived":false,"fork":false,"pushed_at":"2019-04-02T21:21:44.000Z","size":27,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-19T23:49:04.182Z","etag":null,"topics":["pip-package","pypi-package","pypi-packages","python-3","python-package","python-sqlite","python-sqlite3","sqlite","sqlite-database","sqlite3"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/sqlitemap/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eigenein.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-27T18:30:34.000Z","updated_at":"2023-12-25T07:34:58.000Z","dependencies_parsed_at":"2022-09-26T17:40:33.981Z","dependency_job_id":null,"html_url":"https://github.com/eigenein/sqlitemap","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eigenein%2Fsqlitemap","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eigenein%2Fsqlitemap/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eigenein%2Fsqlitemap/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eigenein%2Fsqlitemap/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eigenein","download_url":"https://codeload.github.com/eigenein/sqlitemap/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eigenein%2Fsqlitemap/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259033822,"owners_count":22795769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["pip-package","pypi-package","pypi-packages","python-3","python-package","python-sqlite","python-sqlite3","sqlite","sqlite-database","sqlite3"],"created_at":"2024-10-02T16:34:51.606Z","updated_at":"2025-06-10T08:06:37.410Z","avatar_url":"https://github.com/eigenein.png","language":"Python","readme":"# `sqlitemap`\n\nDictionary interface to an SQLite database.\n\n[![Build Status](https://travis-ci.com/eigenein/sqlitemap.svg?branch=master)](https://travis-ci.com/eigenein/sqlitemap)\n[![Coverage Status](https://coveralls.io/repos/github/eigenein/sqlitemap/badge.svg?branch=master)](https://coveralls.io/github/eigenein/sqlitemap?branch=master)\n[![PyPI - Downloads](https://img.shields.io/pypi/dm/sqlitemap.svg)](https://pypi.org/project/sqlitemap/)\n[![PyPI – Version](https://img.shields.io/pypi/v/sqlitemap.svg)](https://pypi.org/project/sqlitemap/#history)\n[![PyPI – Python](https://img.shields.io/pypi/pyversions/sqlitemap.svg)](https://pypi.org/project/sqlitemap/#files)\n[![License](https://img.shields.io/pypi/l/sqlitemap.svg)](https://github.com/eigenein/sqlitemap/blob/master/LICENSE)\n\n## Intro\n\n…One day I needed an embedded key-value store for a pet project, but didn't find a «good enough» implementation. So, I made my own one.\n\nIt's a lightweight wrapper over the standard [sqlite3](https://docs.python.org/3/library/sqlite3.html) module. It provides the standard [`MutableMapping`](https://docs.python.org/3/library/typing.html#typing.MutableMapping) interface for an SQLite connection and SQLite table.\n\n## `Connection`\n\nYou create an instance of `Connection` as if it was a normal [`sqlite3.connect`](https://docs.python.org/3/library/sqlite3.html#sqlite3.connect) call:\n\n```python\nfrom sqlitemap import Connection\n\nconnection = Connection(':memory:', ...)\n```\n\nIt implements the [context manager](https://docs.python.org/3/library/stdtypes.html#typecontextmanager) interface, so you use `with` to make a transaction as if it was an [`sqlite3.Connection`](https://docs.python.org/3/library/sqlite3.html#sqlite3.Connection). And it implements `MutableMapping[str, Collection]`, except for `__setitem__`. So you can imagine a `Connection` as a dictionary of collections altogether with their [names](https://stackoverflow.com/questions/3694276/what-are-valid-table-names-in-sqlite) and do virtually everything you could do with a normal [`dict`](https://docs.python.org/3.7/library/stdtypes.html#dict):\n\n```python\nfrom sqlitemap import Collection\n\n# Collection is automatically created:\nfoo: Collection = connection['foo']\n\n# You can iterate over collection names:\nnames = list(connection)\n\n# Or even over collections:\ncollections = connection.values()\n\n# Drop collection:\ndel connection['foo']\n\n# Get number of collections:\nlen(connection)\n\n# Special one, to close the connection:\nconnection.close()\n```\n\nInternally, collection is a table with two columns: `key: str` and `value: bytes`. So, you need some serialization to represent objects as byte strings. By default, `sqlitemap` uses the standard [`json`](https://docs.python.org/3/library/json.html) module. It picks up [`ujson`](https://pypi.org/project/ujson/) or [`orjson`](https://pypi.org/project/orjson/), if available. These are also available as `sqlitemap` extras: `sqlitemap[ujson]` and `sqlitemap[orjson]`.\n\nOtherwise, you can specify any custom `Callable[[Any], bytes]` for encoder and `Callable[[bytes], Any]` for decoder:\n\n```python\nconnection = Connection(':memory:', dumps_=custom_dumps, loads_=custom_loads)\n``` \n\n## `Collection`\n\n`Collection` also implements the [context manager](https://docs.python.org/3/library/stdtypes.html#typecontextmanager) interface to make a transaction, and `MutableMapping[str, Any]`:\n\n### Setting an item\n\n```python\nwith raises(KeyError):\n    _ = collection['foo']\ncollection['foo'] = 'bar'\nassert collection['foo'] == 'bar'\ncollection['foo'] = 'qux'\nassert collection['foo'] == 'qux'\n```\n\n`key` column is a primary key.\n\n### Retrieving keys\n\n```python\nassert list(collection) == []\ncollection['foo'] = 'bar'\nassert list(collection) == ['foo']\n```\n\n### Retrieving values\n\n```python\nassert collection.values() == []\ncollection['foo'] = 'bar'\nassert collection.values() == ['bar']\n```\n\n### Deleting an item\n\n```python\nwith raises(KeyError):\n    del collection['foo']\ncollection['foo'] = 42\ndel collection['foo']\nwith raises(KeyError):\n    del collection['foo']\n```\n\n### Using slices\n\n`Collection.__getitem__` and `Collection.__setitem__` also support [slices](https://docs.python.org/3/library/functions.html#slice) as their arguments. Slice `start` is then converted to `key \u003e= start` clause, `stop` to `key \u003c stop` and `step` to `key LIKE step`. All of these are combined with the `AND` operator. `Collection.__getitem__` also applies `ORDER BY key` clause, so it's possible to make some more sophisticated queries:\n\n```python\ncollection['bar'] = 1\ncollection['foo'] = 2\ncollection['quw'] = 3\ncollection['qux'] = 4\ncollection['quy'] = 5\ncollection['quz'] = 6\nassert collection['foo':] == [2, 3, 4, 5, 6]\nassert collection[:'foo'] == [1]\nassert collection[::'qu%'] == [3, 4, 5, 6]\nassert collection['bar':'quz':'qu%'] == [3, 4, 5]\n```\n\nThe same also works with `del collection [...]`. It deletes the rows that would be selected with the corresponding `__getitem__` call:\n\n```python\ncollection['bar'] = 1\ncollection['foo'] = 2\ncollection['quw'] = 3\ncollection['qux'] = 4\ncollection['quy'] = 5\ncollection['quz'] = 6\ndel collection['bar':'quz':'qu%']\nassert list(collection) == ['bar', 'foo', 'quz']\n```\n\n## Controlling transactions\n\n`sqlitemap` does nothing special to control transactions. For that refer to [the standard library documentation](https://docs.python.org/3/library/sqlite3.html#controlling-transactions).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feigenein%2Fsqlitemap","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feigenein%2Fsqlitemap","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feigenein%2Fsqlitemap/lists"}