Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl & DBM Databases

Question regarding the extraction of content from a DBM Database

         

gnotellaluvr

2:47 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Here is a question that I hope someone can help with. I have a project that I am working on for a college class and I desperately need some information. The book covers writing, deleting and modifying information within a DBM database. The project, however, asks for you to retrieve the names and comments from the database and display on a web page. This is the part the book does not cover.

Can anyone provide a simple explanation as to how to do this so I can finish this chapter.

SeanW

3:03 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Been ages since I've had to do this and I don't have a reference handy...

It used to be (Perl 4) that you could tie() a dbmfile to a hash, something on the order of

[perl]
tie my %hash, "foo.dbm" or die "$@";

foreach my $i (%hash) {
print "$i = " . $hash{$i} . "\n";
}
[/dbm]

I think that's been deprecated in favour of a module. Check CPAN for modules with DBM (i /dbm/) or see if there is something like DBM.pm on your system and then read the perldoc.

Sean

Birdman

3:41 pm on Jun 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could use the DB_File module:

use strict;

use DB_File [perl.com];

my $file = 'file.dbm';
my %db;

tie(%db, 'DB_File', $file)
or die "Can't tie $file: $!";

while (my($k, $v) = each %db) {
print '$k = $v\n';
}

That is a very basic example. check out that link for some good info and different ways to use it.

gnotellaluvr

11:30 pm on Jun 12, 2005 (gmt 0)

10+ Year Member



Thank you for the response. I just want to show you the code I use to save to the dbm file. I will show the first few lines of my file and then the actual code that saves to the dbm:

[b]
use SDBM_File;
use Fcntl;
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
[/b]


[b]
sub save_to_dbase {
#declare variables
my %comments;
#open dbase, add record, close dbase
tie(%comments, "SDBM_File", "commentslist", O_CREAT¦O_RDWR, 0666)
or die "Error opening commentslist. $!, stopped";
$comments{email} = "$name¦$comments";
untie(%comments);
[/b]

This creates 2 files in my folder:
commentslist.dir
commentslist.pag

gnotellaluvr

2:44 am on Jun 13, 2005 (gmt 0)

10+ Year Member



What I meant to say and didn't finish typing was, would I basically use that same tie statement and just use the while or for statement?

Birdman

10:34 am on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is correct! Let us know how it goes.

gnotellaluvr

3:38 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



Will do bird. I am so glad that I found this forum. There was another forum that I used to visit alot and if you just mentioned the word homework in your forum they refused to help. But you guys understand that the people that are learning today are the ones who will be helping tomorrow. I will let you know how this works and if I have any other problems.

gnotellaluvr

12:07 am on Jun 14, 2005 (gmt 0)

10+ Year Member



Alright, I have the form working now except for one thing. The first entry shows on the page like so:

Name: email
Comments:

What am I doing wrong? Here is the code I used to display these comments on the page:


sub create_comments_page{
#declare variables
my %commentslist;
my ($name, $comment, $rec);
#open db in a read only format
tie(%commentslist, "SDBM_File", "commentslist", O_RDONLY, 0666)
or die "Error opening commentslist. $!, stopped";
#print statements to web page
print "<HTML><HEAD><TITLE>International Coffees Responses</TITLE></HEAD>\n";
foreach my $rec (%commentslist){
chomp($rec);
($name,$comment) = split(/\¦/,$rec);
print "<B>Name:</B> $name<BR>\n";
print "<B>Comments:</B> $comment<BR>\n";
print "<HR>";
}
print "</BODY>\n";
print "</HTML>\n";
}#end create_comments_page

Birdman

11:34 am on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you are trying to split the key, rather than the value. When you create the hash, you used {email} as the key and that's why it's appearing in your HTML.

Try this instead:

while (my($key, $val) = each %commentslist){

($name,$comment) = split(/\¦/,$val);
print "<B>Name:</B> $name<BR>\n";
print "<B>Comments:</B> $comment<BR>\n";
print "<HR>";
}

Birdman

11:39 am on Jun 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello again,

I would actually change the first script in this way. The difference is, you are using the key to hold the name then just putting comment as value. You defeat the purpose by sticking them together with a pipe(¦) and then splitting them up later.

sub save_to_dbase {
#declare variables
my %comments;
#open dbase, add record, close dbase
tie(%comments, "SDBM_File", "commentslist", O_CREAT¦O_RDWR, 0666)
or die "Error opening commentslist. $!, stopped";
$comments{$name} = $comments;
untie(%comments);

while (my($name, $comment) = each %commentslist){
print "<B>Name:</B> $name<BR>\n";
print "<B>Comments:</B> $comment<BR>\n";
print "<HR>";
}