Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 14 Feb 2001 19:13:14 +0300 (MSK)
From:      Andrey Rouskol <anry@sovintel.ru>
To:        freebsd-current@freebsd.org
Subject:   db interface question
Message-ID:  <Pine.BSF.4.21.0102141905110.419-100000@anry.sovintel.ru>

next in thread | raw e-mail | index | archive | help
Hi All,

I'm trying to write a simple perl program to create a .db file, insert
some values and delete some. The problem I have is that everything happy
during inserting but when I try to remove all key-value pairs sequently
from the .db file my program crashes. I've tested it in FreeBSD-2.8,
FreeBSD-4.2 and FreeBSD-CURRENT. Here are two programs - one to create .db
file and another to delete all elements from it:

--dbcreate - create a database with 100 elements
#!/usr/bin/perl -w

use DB_File ;
use Fcntl ;

my $dbfile = shift;
if( !defined $dbfile) {
	print STDERR "Usage: $0 <db-file> to list\n";
	exit 1;
}

$X = tie( %mydb, DB_File, $dbfile, O_RDWR|O_CREAT, 0666, $DB_HASH );

my $key = "a00";
my $val = "b00";
for(my $i=0; $i<100; $i++) {
	print "$key - $val\n";
	$mydb{$key++} = $val++;
}

undef $X;
untie %mydb;

exit 0;

-- end of dbcreate ---
-- dbclear -- removes all elements from the database
#!/usr/bin/perl -w

use DB_File ;
use Fcntl ;

my $dbfile = shift;
if( !defined $dbfile) {
	print STDERR "Usage: $0 <db-file> to list\n";
	exit 1;
}

$X = tie( %mydb, DB_File, $dbfile, O_RDWR, 0666, $DB_HASH );

my $usr = 0;
my $pw = 0;
my $i = 0;
while( ($usr,$pw) = each %mydb) {
	print "${usr} - ${pw}: deleted\n";
	delete $mydb{$usr};
	++$i;
}
print "$i elements deleted\n";

undef $X;
untie %mydb;

exit 0;
--- end ---

Regards,
-------------------------------------------------------
Andrey Rouskol

Sovintel




To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-current" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.21.0102141905110.419-100000>