March 1, 2021
Hot Topics:

Utilizing Gdbm, GNU's Easy-to-Use Database Management Library

  • By Jay Link
  • Send Email »
  • More Articles »

Lately, a number of open source relational databases have received favorable press. Indeed, MySQL and PostgreSQL have been touted as capable alternatives to Oracle and Sybase, the traditional heavyweights of the commercial RDBMS world.

But let's say you, the programmer, need something simpler. You may have outgrown plain-text "flatfiles," but you might not be quite ready for the planning and maintenance that goes along with a full-fledged RDBMS.

Further, as you likely run Linux or some version of BSD, Microsoft Access isn't aviable option, either. Isn't there an intermediate data storage solution, somewhere between clunky text files and full-blown relational databases?

Fortunately, the answer is yes: Gdbm!

Gdbm, or the GNU Database Manager, is a fully-capable, yet simple means of storing and retrieving data. There aren't 1,001 features and procedures to memorize, making for a gentle learning curve. Tuning is not an issue, and you don't need sophisticated algorithms to get the most out of your data store. Yet, you can still use gdbm-powered databases for customer files, back ends to Websites, and anywhere else that an RDBMS would be appropriate.

On the downside, the speed isn't as great as it might be with other systems. Databases created with gdbm aren't indexed, meaning that you certainly don't want to use it for a multi-terabyte data warehouse. But, for small to mid-sized data collections, the time differential is negligible.

Best of all, you don't need to worry about mounting and unmounting anything created with gdbm. Nor is there any server to run. Your data is simply stored as a regular file on disk.

Now, how do we use it?

The first step is to make sure that you have gdbm. Most Linux distributions include it, but you can make a quick check by looking in your /lib directory. If you see something along the lines of libgdbm.so. (e.g., libgdbm.so.1.7.3), you're in good shape. If not, then all you have to do is make a quick trip to www.gnu.org. Simply download, compile, and install, as you would with any other package.

Now that you have the gdbm library, we'll jump right in and do some coding.

You access the gdbm library by creating C programs that use functions found in the gdbm.h header file. As with other libraries, you link with a simple command line flag -- in this case, -lgdbm.

Gdbm Functions

Creating a new database and opening an existing one are both accomplished with the same function: gdbm_open(). Here's how it works:
   GDBM_FILE dbf;   dbf = gdbm_open(name, block_size, flags, mode, fatal_func);
The "flags" argument is what differentiates between creating a new database and opening an old one. But let's not get out of sequence. Here, then, are the arguments and explanations:
char *name  -- The name of your database. This will be the                name of the binary data file that's written                to disk.int block_size  -- A block is a group of bits that's                    transferred as a single unit. In this                    case, 512 is both the minimum and the                    default. Once the database is created,                    the block size cannot be changed.int flags  --   GDBM_READER   Opens the database in read-only mode.                  Multiple programs can access the                  database while in GDBM_READER mode.   GDBM_WRITER   Allows both reading and writing. Only                  one program can access the database                  in GDBM_WRITER mode.   GDBM_WRCREAT  Creates a new database if none                  previously exists; otherwise the same                  as GDBM_WRITER.   GDBM_NEWDB    Creates a new database regardless of                  whether another one with the same                  name exists (i.e., it will overwrite                  old databases); otherwise, it's the                  same as GDBM_WRITER.int mode  -- This sets the file permissions of the              database, just as you would with chmod.              A sample value would be 0644. void (*fatal_func) ()  -- A function for gdbm to call                           if it detects a fatal error.                           The only parameter of this is                           a string. If the value of NULL                           is provided,gdbm will use a                           default function.
Finally, gdbm_open()'s return value, dbf, is a pointer. This is needed by all the other gdbm functions to access the opened file. If the return value is NULL, however, then your gdbm_open() was unsuccessful. When you're finished with your database, just call:
That's easy enough, isn't it?

Page 1 of 3

This article was originally published on January 4, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date