Snapshot of piskvorky/sqlitedict: 1.2k★, Python, Backend. Persistent dict, backed by sqlite3 and pickle, multithread-safe.
Snapshot summary built from the project's own GitHub metadata — there's no written TopGit review yet. The page will update automatically when a full review is published.
WHY NO REVIEW YET
TopGit writes full reviews for the most-starred, most-requested repositories. This page is a snapshot until then — see the READ ME tab for the original README in full.
A lightweight wrapper around Python's sqlite3 database with a simple, Pythonic
dict-like interface and support for multi-thread access:
Usage
Write
.. code-block:: python
>>> from sqlitedict import SqliteDict
>>> db = SqliteDict("example.sqlite")
>>>
>>> db["1"] = {"name": "first item"}
>>> db["2"] = {"name": "second item"}
>>> db["3"] = {"name": "yet another item"}
>>>
>>> # Commit to save the objects.
>>> db.commit()
>>>
>>> db["4"] = {"name": "yet another item"}
>>> # Oops, forgot to commit here, that object will never be saved.
>>> # Always remember to commit, or enable autocommit with SqliteDict("example.sqlite", autocommit=True)
>>> # Autocommit is off by default for performance.
>>>
>>> db.close()
Read
.. code-block:: python
>>> from sqlitedict import SqliteDict
>>> db = SqliteDict("example.sqlite")
>>>
>>> print("There are %d items in the database" % len(db))
There are 3 items in the database
>>>
>>> # Standard dict interface. items() values() keys() etc...
>>> for key, item in db.items():
... print("%s=%s" % (key, item))
1={'name': 'first item'}
2={'name': 'second item'}
3={'name': 'yet another item'}
>>>
>>> db.close()
Efficiency
By default, sqlitedict's exception handling favors verbosity over efficiency.
It extracts and outputs the outer exception stack to the error logs.
If you favor efficiency, then initialize the DB with outer_stack=False.
.. code-block:: python
>>> from sqlitedict import SqliteDict
>>> db = SqliteDict("example.sqlite", outer_stack=False) # True is the default
>>> db[1]
{'name': 'first item'}
Context Manager
.. code-block:: python
>>> from sqlitedict import SqliteDict
>>>
>>> # The database is automatically closed when leaving the with section.
>>> # Uncommitted objects are not saved on close. REMEMBER TO COMMIT!
>>>
>>> with SqliteDict("example.sqlite") as db:
... print("There are %d items in the database" % len(db))
There are 3 items in the database
Tables
A database file can store multiple tables.
A default table is used when no table name is specified.
Note: Writes are serialized, having multiple tables does not improve performance.
In case you're wondering, the unnamed table comes from the previous examples,
where we did not specify a table name.
Serialization
Keys are strings. Values are any serializeable object.
By default Pickle is used internally to (de)serialize the values.
It's possible to use a custom (de)serializer, notably for JSON and for compression.
.. code-block:: python
>>> # Use JSON instead of pickle
>>> import json
>>> with SqliteDict("example.sqlite", encode=json.dumps, decode=json.loads) as mydict:
... pass
>>>
>>> # Apply zlib compression after pickling
>>> import zlib, pickle, sqlite3
>>>
>>> def my_encode(obj):
... return sqlite3.Binary(zlib.compress(pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)))
>>>
>>> def my_decode(obj):
... return pickle.loads(zlib.decompress(bytes(obj)))
>>>
>>> with SqliteDict("example.sqlite", encode=my_encode, decode=my_decode) as mydict:
... pass
It's also possible to use a custom (de)serializer for keys to allow non-string keys.
.. code-block:: python
>>> # Use key encoding instead of default string keys only
>>> from sqlitedict import encode_key, decode_key
>>> with SqliteDict("example.sqlite", encode_key=encode_key, decode_key=decode_key) as mydict:
... pass
More
Functions are well documented, see docstrings directly in sqlitedict.py or call help(sqlitedict).
Beware: because of Python semantics, sqlitedict cannot know when a mutable
SqliteDict-backed entry was modified in RAM. You'll need to
explicitly assign the mutated object back to SqliteDict:
.. code-block:: python
>>> from sqlitedict import SqliteDict
>>> db = SqliteDict("example.sqlite")
>>> db["colors"] = {"red": (255, 0, 0)}
>>> db.commit()
>>>
>>> colors = db["colors"]
>>> colors["blue"] = (0, 0, 255) # sqlite DB not updated here!
>>> db["colors"] = colors # now updated
>>>
>>> db.commit() # remember to commit (or set autocommit)
>>> db.close()
Features
Values can be any picklable objects (uses pickle with the highest protocol).
Support for multiple tables (=dicts) living in the same database file.
Support for access from multiple threads to the same connection (needed by e.g. Pyro).
Vanilla sqlite3 gives you ProgrammingError: SQLite objects created in a thread can only be used in that same thread.
Concurrent requests are still serialized internally, so this "multithreaded support"
doesn't give you any performance benefits. It is a work-around for sqlite limitations in Python.
sqlite is efficient and can work effectively with large databases (multi gigabytes), not limited by memory.
sqlitedict is mostly a thin wrapper around sqlite.
items()keys()values() are iterating one by one, the rows are loaded in a worker thread and queued in memory.
len() is calling sqlite to count rows, that is scanning the whole table.
For better performance, write objects in batch and commit() once.
Installation
The module has no dependencies beyond Python itself.
The minimum supported Python version is 3.7, continuously tested on Python 3.7, 3.8, 3.9, and 3.10 on Travis <https://travis-ci.org/RaRe-Technologies/sqlitedict>_.
Install or upgrade with::
pip install -U sqlitedict
or from the source tar.gz <http://pypi.python.org/pypi/sqlitedict>_::
sqlitedict resides on github <https://github.com/RaRe-Technologies/sqlitedict>_. You can file
issues or pull requests there.
License
sqlitedict is open source software released under the Apache 2.0 license <http://opensource.org/licenses/apache2.0.php>.
Copyright (c) 2011-now Radim Řehůřek <http://radimrehurek.com> and contributors.
Housekeeping
Clean up the test database to keep each doctest run idempotent:
.. code-block:: python
import os
if name == 'main':
... os.unlink('example.sqlite')
No homepage URL was recorded for piskvorky/sqlitedict in TopGit's last sync. The README tab above frequently contains screenshots and demo links, or check the repository description on GitHub.
How active is development on piskvorky/sqlitedict?
The most recent commit recorded on piskvorky/sqlitedict was 3.7 years ago, based on the GitHub push timestamp. The repository has 140 forks — one of the better signals of community interest.
Is piskvorky/sqlitedict open source?
Yes — piskvorky/sqlitedict ships under the Apache-2.0 license, which makes its source code freely readable (and, depending on license terms, forkable and reusable). Source: github.com/piskvorky/sqlitedict.
What license does piskvorky/sqlitedict use?
piskvorky/sqlitedict is released under the Apache-2.0 license. Always verify the LICENSE file directly on GitHub for the authoritative terms — license strings can be edited out of sync with a project's actual stance.
What topics is piskvorky/sqlitedict associated with?
GitHub's repository topics for piskvorky/sqlitedict: "data-store", "multi-threading", "python", "sqlite". TopGit's editorial category is Backend.
Where do I read more about piskvorky/sqlitedict?
This TopGit page is a snapshot — the READ ME tab shows the project's own README content (links stripped, images preserved). The GitHub repository at github.com/piskvorky/sqlitedict is the definitive source.
Read full README in the tab above.
Want a second opinion on sqlitedict?
Ask an AI that can read this page — one click and you get its take on sqlitedict.