'Use SQLite as a key:value store

As suggested in comments from Key: value store in Python for possibly 100 GB of data, without client/server and in other questions, SQLite could totally be used as a persistent key:value store.

How would you define a class (or just wrapper functions) such that using a key:value store with SQLite would be as simple as:

kv = Keyvaluestore('/test.db')
kv['hello'] = 'hi'        # set
print(kv['hello'])         # get
print('blah' in kv)        # answer: False because there's no key 'blah' in the store
kv.close()

?



Solution 1:[1]

There is already sqlitedict which appears to meet all your needs.

From the documentation:

from sqlitedict import SqliteDict
mydict = SqliteDict('./my_db.sqlite', autocommit=True)
mydict['some_key'] = any_picklable_object
print(mydict['some_key'])  # prints the new value
for key, value in mydict.iteritems():
    print(key, value)
print(len(mydict))  # etc... all dict functions work
mydict.close()

Solution 2:[2]

I liked Basj's answer, but I'd also like to add the following function in the KeyValueStore class so that while using the DB, we can commit the data without necessarily closing the db.

class KeyValueStore(dict):
    """Other functions"""
    def commit(self):
        self.conn.commit()

Solution 3:[3]

sqlite3dbm provides this functionality, while keeping the traditional dbm interface. It is a simpler interface than sqlitedict, for when you just need a very basic key-value store. It also includes a convenience class that can store arbitrary objects as the value using python's shelve functionality.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Basj
Solution 2 Arnav Kamath
Solution 3 Brian Minton