[Chapter 17] Database Manipulation

PreviousChapter 17Next

17. Database Manipulation

DBM Databases and DBM Hashes
Opening and Closing DBM Hashes
Using a DBM Hash
Fixed-Length Random-Access Databases
Variable-Length ( Text) Databases
Win32 Database Interfaces

17.1 DBM Databases and DBM Hashes

Most UNIX systems have a standard library called DBM that many Win32 programmers have never heard about. This library provides a simple database management facility that allows programs to store a collection of key-value pairs into a pair of disk files. These files retain the values in the database between invocations of the programs using the database, and these programs can add new values, update existing values, or delete old values.

The DBM library is fairly simple, but being readily available, it has been used for many programs with modest database needs. For example, the famous UNIX mail program, sendmail (and its variants and derivatives), stores its user alias database (the mapping of mail addresses to recipients) as a DBM database. The most popular Usenet news software uses a DBM database to track current and recently seen articles. In spite of this, it is unlikely that you will have DBM files laying around on your Windows system (unless you've already created them using Perl).

Perl provides access to this same DBM mechanism through a rather clever means: a hash can be associated with a DBM database through a process similar to opening a file. This hash (called a DBM array) is then used to access and modify the DBM database. Creating a new element in the array modifies the DBM database immediately. Deleting an element deletes the value from the DBM database, and so on.[1]

[1] This case is actually just a special use of the general tie mechanism. If you want something more flexible, check out the SDBM_File and perltie documentation.

The size, number, and kind of keys and values in a DBM database are restricted, and depending on which version of DBM library you're using, a DBM array may share these same restrictions. Perl for Win32 includes the SDBM database routines. In general, if you keep both the keys and the values down to 1000 arbitrary binary characters or less, you'll probably be OK.

16.7 ExercisesBook Index17.2 Opening and Closing DBM Hashes

© 2000- NIV