January 22, 2017
Hot Topics:

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

  • January 4, 2001
  • By Jay Link
  • Send Email »
  • More Articles »

Deleting an Entire Database

Remembering that gdbm databases are comprised of single files; all the programmer has to do to delete a database is to delete that one file. Trivial!

The gdbm man page lists a few other functions, but I'm not going to cover them here, as they're not imperative to the operation of a gdbm database, and this is just a simple overview.

Can the functionality of gdbm be accessed via shell scripts? The answer is yes, but it takes a little bit of ingenious manipulation. What you'll need to do is create some programs that accept command line arguments, and then pass those values on to the gdbm functions. For example, let's make a simple program that allows you to populate a database from the command line. Here's how it might look:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <gdbm.h>#define BLOCK_SIZE  512#define MODE        0644int main(int argc, char *argv[]){   int ret;   datum key;   datum value;   GDBM_FILE dbf;   if (argc < 4)   {      printf("Usage: insert <database name> <key> <value>\n");      exit(0);   }   key.dptr = argv[2];   key.dsize = strlen(argv[2]);   value.dptr = argv[3];   value.dsize = strlen(argv[3]);   dbf = gdbm_open(argv[1], BLOCK_SIZE, GDBM_WRCREAT, MODE, NULL);   ret = gdbm_store(dbf, key, value, GDBM_INSERT);   gdbm_close(dbf);   if (ret == 1)   {      printf("That key already exists.\n");   }   return EXIT_SUCCESS;}
If this program were named insert.c, then you'd compile it with:
   gcc -O3 -Wall -o insert insert.c -lgdbm
Then, to use it, all you'd have to do is type the following:
   insert <database name> <key> <value>
For example:
   insert employees name1 Mary
Now, if the value for "name" were "Mary Smith," then you'd have a problem. You could solve this by making two values, firstname1 and lastname1, or you could modify insert.c to look for ALL the command line arguments given after the name of the key, which you could then lump into one value. Either way, it's hardly insurmountable.

I'm sure you could now figure out how to make another program to retrieve specific key values, using gdbm_fetch()!

In conclusion, I find gdbm to be an excellent tool for creating small data files. Either used within C programs, or called from the shell using a program such as include.c, gdbm functions are much easier to work with than the tedious alternative of opening a text file and searching through line after line. This is especially true when dealing with bash scripts!

Related Resources

1. GNU's home page This is the home page for the GNU project. You can download gdbm -- including the source code -- from here.

2. The Linux Documentation Project The LDP is a vast storehouse of knowledge. More information on gdbm can be found in the ELF-HOWTO guide.

3. PostgreSQL home page When you're ready to move up to a full RDBMS, check out PostgreSQL. Pay no attention to the silly name!

About Author


Page 3 of 3

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

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

Sitemap | Contact Us

Rocket Fuel