anydbm, gdbm, dbm

Discussion in 'Python' started by Lacrima, Oct 27, 2009.

  1. Lacrima

    Lacrima Guest

    Hello!

    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?

    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?

    Thanks in advance...
     
    Lacrima, Oct 27, 2009
    #1
    1. Advertising

  2. Lacrima

    Tim Chase Guest

    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
     
    Tim Chase, Oct 27, 2009
    #2
    1. Advertising

  3. Lacrima

    Lacrima Guest

    On Oct 27, 4:36 pm, Tim Chase <> wrote:
    > 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


    Hi!

    Thanks a lot! You've helped me very much!
     
    Lacrima, Oct 28, 2009
    #3
  4. In message <b0ec6512-ac80-4372-
    >, Lacrima wrote:

    > 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?


    Maybe you should look at sqlite instead.
     
    Lawrence D'Oliveiro, Oct 28, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John D.

    Example code for anydbm wanted...

    John D., Aug 4, 2003, in forum: Python
    Replies:
    0
    Views:
    340
    John D.
    Aug 4, 2003
  2. Gary Richardson

    anydbm bug ?

    Gary Richardson, Dec 4, 2003, in forum: Python
    Replies:
    2
    Views:
    590
    Dennis Lee Bieber
    Dec 5, 2003
  3. =?ISO-8859-1?Q?=22C=E9dric_V=2E=22?=

    error at "import anydbm"

    =?ISO-8859-1?Q?=22C=E9dric_V=2E=22?=, Jan 13, 2004, in forum: Python
    Replies:
    4
    Views:
    565
    Peter Hansen
    Jan 14, 2004
  4. Anton Sherwood

    text storage: shelve vs anydbm vs ?

    Anton Sherwood, Apr 17, 2004, in forum: Python
    Replies:
    1
    Views:
    451
    Larry Bates
    Apr 17, 2004
  5. Derek Basch
    Replies:
    1
    Views:
    411
    Dan Sommers
    Jan 11, 2005
Loading...

Share This Page