Question(s) about ndbm..

Question(s) about ndbm..

Post by Prakash Mahe » Sun, 30 Jun 1996 04:00:00

If any of you have used the ndbm/dbm database library
that comes with most versions of UNIX, please anwer these
        1. How do you create keys.
        2. What about the flags and modes for the functions
The 'man' for ndbm is not really good, and I scratched my head
for 3 hours today with partial success, to create a database
with this tool.
I would appreciate if you can give me some tips on this.

Thanks in advance.

    Prakash Mahesh                  


Question(s) about ndbm..

Post by Liam R. E. Qu » Tue, 02 Jul 1996 04:00:00

>If any of you have used the ndbm/dbm database library
>that comes with most versions of UNIX, please anwer these
>    1. How do you create keys.
>    2. What about the flags and modes for the functions

dbm is a key-valu-pair database.  What I mean by that is that you
store things by a key, and later, given the key, you can get them
back again.  A more common example of a key-value-pair database is a
filesystem: you store data in a file, and the key is the filename.
Later, you can type in the filename and get the data back.


Here is some untested code:

#include <stdio.h>
#include <ndbm.h>

typedef struct {
    char *dptr;
    int dsize;

Quote:} datum;

    datum dbmKey, dbmContent;
    DBM *dbm;

    dbm = dbm_open("boys", O_RDWR|O_CREAT, 0664);
    if (!dbm) {
        perror("couldn't open dbm file");
    dbmKey.dptr = "Simon";
    dbmKey.dsize = strlen("Simon") + 1; /* i.e. sizeof("Simon"), incl. NUL */

    dbmContent.dptr = "A good and close friend of mine.";
    dbmContent.dsize = strln(dbmContent.dptr) + 1; /* include the NUL */

    (void) dbm_store(dbm, key, content, DBM_REPLACE);

    /* fetch it back again */

    dbmContent = dbm_fetch(dbm, dbmKey);
    printf("%*.*s: %*.*s\n",
        dbmKey.dsize, dbmKey.dsize, dbmKey.dptr,
        dbmContent.dsize, dbmContent.dsize, dbmContent.dptr

    return 0;


You can use anything you like for the key.
Any good book on Unix programming (Richard Stevens?) should discuss ndbm,
although I don't know which ones actually do.


Liam Quin, SoftQuad Inc    | lq-text freely available Unix text retrieval

SGML:   |`Consider yourself... one of the family...
The barefoot programmer    | consider yourself... At Home!' [the Artful Dodger]


1. Questions about ndbm/gdbm..

First of all thanks to all of you who had replied to me regarding my earlier
questions. I replied personally to most of you, but not all.

I tried writing programs using ndbm/gdbm. I know that it is a database
where you can only have a key and a contents for the key. My kind of
application will have more fields, may be 10-20. So can I modify,
or adapt ndbm for my application. I figured out you can do it in two ways..

1. Have many files, and the contents of first file, hashes into the second
and so on. This means that I will have n+1 files for n fields in my database,
which is abuse of resources I guess.
2. Hash into the same file (Example..
                key     Contents
                1       One
                One     Two
                Two     Three
                Three   End (or any special set of chars.)

        This is kinda complex, as I have to find the special char. (End)
everytime, and if there are more than one key with same value then the
whole setup is confusing.

Can someone tell me if there is a better way to use ndbm for applications
with more multiple fields.
I would appreciate your reply.
Thanks in advance.

    Prakash Mahesh, Software Engineer.              

2. Using 3 Tables

3. JAVA access to dbm / ndbm files

4. VB 6.0, SQL Server 7.0 and Images

5. Help: ndbm hash table

6. How do I base a formview on a combo box selection?

7. Problem with ndbm database size

8. need ndbm error translated

9. Whats wrong with Unix NDBM?

10. Does anybody know where to obtain ndbm source?

11. ndbm

12. ndbm <--- What is it for ?