A database is a file that is organized for storing data. Many databases are organized like a dictionary in the sense that they map from keys to values. The biggest difference between a database and a dictionary is that the database is on disk (or other permanent storage), so it persists after the program ends.
dbm provides an interface for creating and updating database files. As an example, I’ll create a database that contains captions for image files.
Opening a database is similar to opening other files:
>>> import dbm >>> db = dbm.open('captions', 'c')
'c' means that the database should be created if it doesn’t already exist. The result is a database object that can be used (for most operations) like a dictionary.
When you create a new item,
dbm updates the database file.
>>> db['cleese.png'] = 'Photo of John Cleese.'
When you access one of the items,
dbm reads the file:
>>> db['cleese.png'] b'Photo of John Cleese.'
The result is a bytes object, which is why it begins with
b. A bytes object is similar to a string in many ways. When you get farther into Python, the difference becomes important, but for now we can ignore it.
If you make another assignment to an existing key,
dbm replaces the old value:
>>> db['cleese.png'] = 'Photo of John Cleese doing a silly walk.' >>> db['cleese.png'] b'Photo of John Cleese doing a silly walk.'
Some dictionary methods, like
items, don’t work with database objects. But iteration with a
for loop works:
for key in db: print(key, db[key])
As with other files, you should close the database when you are done: