Go to the previous, next section.

Searching for records in the database.

Looks up a given key and returns the information associated with that key. The pointer in the structure that is returned is a pointer to dynamically allocated memory block. To search for some data:

content = gdbm_fetch(dbf, key);

The parameters are:

GDBM_FILE dbf
The pointer returned by gdbm_open.
datum key
The key data.

The datum returned in content is a pointer to the data found. If the dptr is NULL, no data was found. If dptr is not NULL, then it points to data allocated by malloc. gdbm does not automatically free this data. The user must free this storage when done using it. This eliminates the need to copy the result to save it for later use (you just save the pointer).

You may also search for a particular key without retrieving it, using:

ret = gdbm_exists(dbf, key);

The parameters are:

GDBM_FILE dbf
The pointer returned by gdbm_open.
datum key
The key data.

Unlike gdbm_fetch, this routine does not allocate any memory, and simply returns true or false, depending on whether the key exists, or not.

Go to the previous, next section.