anydbm, gdbm, dbm

Tim Chase python.list at tim.thechases.com
Tue Oct 27 15:36:22 CET 2009


Lacrima wrote:
> I want to store some data, using anydbm module.
> According to docs the object returned by anydbm.open() supports most
> of the same functionality as dictionaries; keys and their
> corresponding values can be stored, retrieved, and deleted...
> 
> I don't understand how I can make a table in DBM database, or a row in
> a table. Or all data must be stored just as key-value pairs?

Yes, all data for the dbm variants is purely string->string 
mapping pairs.  Similarly, dictionaries don't natively allow you 
to store columns in them...they are just key->value data-stores.

> Suppose my table consists of two columns: 'first name' and 'last
> name'. How can I make such table and write there first and last names
> for, say, two any persons?


*dbm provides no columns unless you hack them such as

   db[key] = DELIMITER.join([lastname, firstname])

and then unhack them:

   lastname, firstname = db[key].split(DELIMITER, 1)

As a variant of this, you might be able to use pickle/shelve to 
stash your multi-content object as a string-value.

Alternatively, you could use something like

   db["%s_first" % key] = firstname
   db["%s_last" % key] = lastname

assuming your keys didn't confound you.

-tkc







More information about the Python-list mailing list