1. sqlite3dbm — Sqlite-backed dbm

Note

Hopefully sqlite3dbm.dbm will be included in Python 3.0, renamed to dbm.sqlite.

This module is quite similar to the dbm module, but uses sqlite3 instead for a backend store. It also provides a small extension to the traditional dictionary interface.

1.1. Module Interface

The module defines the following constant and function:

exception sqlite3dbm.dbm.error

Raised on sqlite3dbm-specific errors, such as protection errors. KeyError is raised for general mapping errors like specifying an incorrect key.

Accessible as sqlite3dbm.error.

sqlite3dbm.dbm.open(filename[, flag[, mode]])[source]

Open a database and return a sqlite3dbm object. The filename argument is the path to the database file.

The optional flag argument can be:

Value Meaning
'r' Open existing database for reading only (default)
'w' Open existing database for reading and writing
'c' Open database for reading and writing, creating it if it doesn’t exist
'n' Always create a new, empty database, open for reading and writing

The optional mode argument is the Unix mode of the file, used only when the database has to be created. It defaults to octal 0666 and respects the prevailing umask.

Accessible as sqlite3dbm.open.

1.2. Extended Object Interface

The underlying object is a SqliteMap. In addition to the standard dictionary methods, such objects have the following methods:

SqliteMap.__getitem__(k)[source]

x.__getitem__(k) <==> x[k]

This version of __getitem__() also transparently works on lists:
>>> smap.update({'1': 'a', '2': 'b', '3': 'c'})
>>> smap['1', '2', '3']
[u'a', u'b', u'c']
>>> smap[['1', '2', '3']]
[u'a', u'b', u'c']
SqliteMap.select(*args)[source]

List based version of __getitem__(). Complement of update().

args are the keys to retrieve from the dict. All of the following work:
>>> smap.update({'1': 'a', '2': 'b', '3': 'c'})
>>> smap.select('1', '2', '3')
[u'a', u'b', u'c']
>>> smap.select(['1', '2', '3'])
[u'a', u'b', u'c']
>>> smap.select(['1', '2'], '3')
[u'a', u'b', u'c']
>>> smap.select(['1', '2'], ['3'])
[u'a', u'b', u'c']
Returns:
List of values corresponding to the requested keys in order
Raises:
KeyError if any of the keys are missing
SqliteMap.get_many(*args, **kwargs)[source]

Basically get() and select() combined.

The interface is the same as select() except for the additional option argument default. This argument specifies what value should be used for keys that are not present in the dict.

1.3. Usage Example

>>> import sqlite3dbm
>>> db = sqlite3dbm.open('mydb.sqlite3', flag='c')
>>>
>>> # Print doesn't work, you need to do .items()
>>> db
<sqlite3dbm.dbm.SqliteMap object at 0x7f0d6ecac4d0>
>>> db.items()
[]
>>>
>>> # Acts like a regular dict
>>> db['foo'] = 'bar'
>>> db['foo']
'bar'
>>> db.items()
[('foo', 'bar')]
>>> del db['foo']
>>> db.items()
[]
>>>
>>> # Some extentions that allow for batch reads
>>> db.update({'foo': 'one', 'bar': 'two', 'baz': 'three'})
>>> db['foo', 'bar']
['one', 'two']
>>> db.select('foo', 'bar')
['one', 'two']
>>> db.select('foo', 'bar', 'qux')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./sqlite3dbm/dbm.py", line 343, in select
    raise KeyError('One of the requested keys is missing!')
KeyError: 'One of the requested keys is missing!'
>>> db.get_many('foo', 'bar', 'qux')
['one', 'two', None]
>>> db.get_many('foo', 'bar', 'qux', default='')
['one', 'two', '']
>>>
>>> # Persistent!
>>> db.items()
[('baz', 'three'), ('foo', 'one'), ('bar', 'two')]
>>> del db
>>> reopened_db = sqlite3dbm.open('mydb.sqlite3')
>>> reopened_db.items()
[('baz', 'three'), ('foo', 'one'), ('bar', 'two')]
>>>
>>> # Be aware that the default flag is 'r'
>>> reopened_db['qux'] = 'four'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./sqlite3dbm/dbm.py", line 164, in __setitem__
    raise error('DB is readonly')
sqlite3dbm.dbm.SqliteMapException: DB is readonly
>>> writeable_db = sqlite3dbm.open('mydb.sqlite3', flag='w') # 'c' would be fine too
>>> writeable_db['qux'] = 'four'
>>> reopened_db.items()
[('baz', 'three'), ('foo', 'one'), ('bar', 'two'), ('qux', 'four')]
>>> writeable_db.items()
[('baz', 'three'), ('foo', 'one'), ('bar', 'two'), ('qux', 'four')]
>>>
>>> # Catching sqlite3dbm-specific errors
>>> try:
...   reopened_db['foo'] = 'blargh'
... except sqlite3dbm.error:
...   print 'Caught a module-specific error'
...
Caught a module-specific error

See also

Module dbm
Standard Unix database interface.
Module gdbm
Similar interface to the GUNU GDBM library.
Module sqlite3dbm.sshelve
Extension of shelve for a salite3dbm.dbm