[Chicago-talk] CLI client for DBM/Berkeley DB databases

Alan Mead amead at alanmead.org
Fri Apr 14 14:36:32 PDT 2006


Hi all,

I'm using Geo::Coder::US...  This module uses Berkeley DB to store
geographic data. I have a db-utils package of tools to dump, verify,
etc. but I'm really missing a command line interface to examine, insert,
update, delete, etc. rows of data (like the mysql or sqlite3
command-line clients).  I once thought I could just whip up a Perl
script for this... but while I have used SQLite and MySQL a lot, I'm
brand new to DBM and so far the learning curve seems sharp.  Here's what
I get when I try the code based on example 14-1 from the Perl cookbook...

[amead at ugly tiger]$ ./db4.pl small_tiger.db
Cannot open "small_tiger.db":
[amead at ugly tiger]$ ls -l small_tiger.db
-rw-rw-r--  1 amead amead 52412416 Apr 14 16:20 small_tiger.db
[amead at ugly tiger]$ ls -dl ../tiger
drwxr-xr-x  7 amead amead 4096 Apr 14 16:20 ../tiger/
[amead at ugly tiger]$ cat db4.pl
#!/usr/bin/perl

use DB_File;
use strict;
use warnings;

my $db = shift or die "usage: $0 filename\n\nPlease supply a DBM
filename\n";
my %db;
tie(%db,'DB_File',$db) or die  "Cannot open \"$db\": $!\n";
my $num_records = scalar keys %db;
print "Found $num_records records\n";
untie %db;

Notice that there's no error message, it just doesn't work.  This file
works fine in conjunction with the Geo::Coder::US package, so doubt that
the DBM file is corrupt.  I've tried other code (I think I found it
online) that uses AnyFile... nothing I've tried even opens the DBM file:

[amead at ugly tiger]$ ./db2.pl small_tiger.db
Can't locate object method "TIEHASH" via package "AnyDBM_File" at
/usr/lib/perl5/site_perl/5.8.5/DBM/Any.pm line 76.
[amead at ugly tiger]$ cat db2.pl
#!/usr/bin/perl

BEGIN {
  @AnyDBM_File::ISA = qw(DB_File GDBM_File SDBM_File);
}
use DBM::Any;
use strict;
use warnings;

my $filename = shift or die "usage: $0 filename\n\nPlease supply a DBM
filename\n";
my $flags = 'O_RDWR';
my $db = new DBM::Any($filename, $flags, 0640) or die "Cannot open
\"$filename\": $!\n";
my $num_records = scalar $db->keys();
print "Found $num_records records\n";

my $key = ($db->keys())[0];
my $value = $db->get($key);

print "sample record:\nkey = $key\nvalue = $value\n\n";

$db->close();


Any help is much appreciated!

-Alan


More information about the Chicago-talk mailing list