Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

NDBM_File won't store data

by HamNRye (Monk)
on Aug 28, 2003 at 15:58 UTC ( [id://287421]=perlquestion: print w/replies, xml ) Need Help??

HamNRye has asked for the wisdom of the Perl Monks concerning the following question:

Monkins,
I am working on a script that polls a database of images and tracks if they are linked to documents. I am trying to use NDBM_File for this (cannot run a DB on the server in question) but I am having trouble with storing the date.

I am using a hash called "%mtime" and assigning the date like $mtime{$image} = $date;. The problem is that when I reopen the DB, it has not stored anything for the date. The image names are in the Hash as keys, but every date is Null.

I assume that the Hash is getting updated, because the following: print "$image => $mtime{$image}\n"; produces the following output: \yy8001.txt => 20030828

my %orphanart = ListImages(); sub ListImages { my (@images, @logos, @graphics, $allimages, %orphans); my ($linked, $unlinked, $imagecount, $graphiccount, $logocount); + my $dbi = new MB::DBI; my $date = MakeDate(); tie %mtime, "NDBM_File", 'mtime', O_RDWR|O_CREAT|O_EXCL, 0644; tie %type, "NDBM_File", 'type', O_RDWR|O_CREAT|O_EXCL, 0644; # Get images if ( $dbi->get_keys("image", \@images)) { wlog("Starting images."); my $icount; foreach $image (@images) { print $spinner; local @tmp; my $e = new MB::Element(type => "image", name => "$image") +; my $id = $e->return_id(); if ($count = $dbi->sql("select Ad_name from dbo.Ad p, dbo. +AdImage j where p.Ad_id = j.Ad_id and j.image_id = $id",\@tmp)) { delete($mtime{$image}); delete($type{$image}); delete($protection{$image}); $linked++; } else { $mtime{$image} = $date; print "$image => $mtime{$image}\n"; $type{$image} = "image"; $orphans{$image} = "image"; $unlinked++; $icount++; } } wlog("Leaving images."); $imagecount = $#images + 1; wlog("$imagecount images exist in the database.\n\t$linked lin +ked images, and $icount orphan images."); } else { $error = $dbi->last_error(); wlog("No data returned for images.\n$error"); } # Get logos if ( $dbi->get_keys("logo", \@logos)) { wlog("Starting logos."); my $icount; foreach $logo (@logos) { print $spinner; local @tmp; my $e = new MB::Element(type => "logo", name => "$logo"); my $id = $e->return_id(); if ($count = $dbi->sql("select Ad_name from dbo.Ad p, dbo. +AdLogo j where p.Ad_id = j.Ad_id and j.logo_id = $id",\@tmp)) { delete($mtime{$logo}); delete($type{$logo}); delete($protection{$logo}); $linked++; } else { $mtime{$logo} = $date; $type{$logo} = "logo"; $orphans{$logo} = "logo"; $unlinked++; $icount++; } } wlog("Leaving logos."); $logocount = $#logos + 1; wlog("$logocount logos exist in the database.\n\t$linked linke +d logos, and $icount orphan logos."); } else { $error = $dbi->last_error(); wlog("No data returned for logos.\n$error"); } # Get graphics if ( $dbi->get_keys("graphic", \@graphics)) { wlog("Starting graphics."); my $icount; #$#graphics = 50; foreach $graphic (@graphics) { print $spinner; local @tmp; my $e = new MB::Element(type => "graphic", name => "$graph +ic"); my $id = $e->return_id(); if ($count = $dbi->sql("select Ad_name from dbo.Ad p, dbo. +AdGraphic j where p.Ad_id = j.Ad_id and j.graphic_id = $id",\@tmp)) { delete($mtime{$graphic}); delete($type{$graphic}); delete($protection{$graphic}); $linked++; } else { $mtime{$graphic} = $date; $type{$graphic} = "graphic"; $orphans{$graphic} = "graphic"; $unlinked++; $icount++; } } wlog("Leaving graphics."); $graphiccount = $#graphics + 1; wlog("$graphiccount graphics exist in the database.\n\t$linked + linked graphics, and $icount orphan graphics."); } else { $error = $dbi->last_error(); wlog("No data returned for graphics.\n$error"); } untie %mtime, %type; my $allimages = $imagecount + $logocount + $graphiccount; my $opct = $unlinked / $allimages; wlog("$allimages total art in the database. $unlinked orphans.\n $ +opct% of the art in the DB is unlinked."); return %orphans; }
Output:
\00_Ford_Focus_ZX3.tif => 20030828 /02_Jeep_Liberty_Sport.tif => 20030828 \apple_turnovers_plate.eps => 20030828 /background_art_cx.psd => 20030828 -backgroundtrade.psd => 20030828 \bagels_assorted.eps => 20030828 /bike image.eps => 20030828 -bike image.tif => 20030828 \bike.jpg => 20030828 |birthday_cake.eps => 20030828 /birthday_cake_single.eps => 20030828 -border_2x1std_kd.psd => 20030828 \bread_french.eps => 20030828 |bread_garlic_cheese.eps => 20030828 /bread_italian.eps => 20030828 -bread_loaves.eps => 20030828 \bread_pita.eps => 20030828 |bread_racksack_king.eps => 20030828 /bread_racksack_rndtp.eps => 20030828 -bread_sourdough.eps => 20030828 \bread_white_sliced.eps => 20030828 |breads_assorted.eps => 20030828

update (broquaint): added <readmore> tag

Replies are listed 'Best First'.
Re: NDBM_File won't store data
by poj (Abbot) on Aug 28, 2003 at 20:07 UTC
    I tested the principle with this code and it worked fine
    use NDBM_File; use Fcntl; tie (%mtime, "NDBM_File",'mtime',O_RDWR|O_CREAT|O_EXCL, 0640) or die " +error : $!"; # dump the data for (sort keys %mtime){ print "$_ $mtime{$_}\n"; } # add a new key $date= MakeDate(); $now = scalar localtime; $mtime{$now} = $date; untie %mtime; sub MakeDate { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); $year = ($year + 1900) ; $mon++ ; my $date = sprintf("%04D%02d%02d", $year, $mon, $mday); return $date; }
    How are you checking the values in the database ?
    poj

      The report function returns nothing. When I read the flat-file, it has aerio_sx_02_c.epsabrewster_pam.epsbbrewster_pam.eps in there. I should see keyvaluekeyvalue not just keykeykey.

      I myself wrote a test function:

      # testing Functions sub NDBM_test { my $date = MakeDate(); #write my %test; tie %test, "NDBM_File", 'test', O_RDWR|O_CREAT|O_EXCL, 0644; foreach $number (1...10) { $test{$number} = $date; } untie %test; #read tie %test, "NDBM_File", 'test', O_RDWR|O_EXCL, 0644; foreach $number (1...10) { print "$number: $test{$number}\n"; } untie %test; }

      And it produces:

      1: 20030828 2: 20030828 3: 20030828 4: 20030828 5: 20030828 6: 20030828 7: 20030828 8: 20030828 9: 20030828 10: 20030828

      Here's what the flat file looks like...

      2003082810200308289200308288200308287200308286200308285200308284200308 +283200308282200308281
        I'm not sure what you mean by 'the report function' or 'the flat-file', are you referring to another piece of code ? To confirm the problem is in the database, run your test code against the database thus ;
        #read tie %test, "NDBM_File", 'mtime', O_RDWR|O_EXCL, 0644; foreach (keys %test) { print "$_: $test{$_}\n"; } untie %test;
        If this shows only keys, try it with the other database 'type'.
        #read tie %test, "NDBM_File", 'type', O_RDWR|O_EXCL, 0644; foreach (keys %test) { print "$_: $test{$_}\n"; } untie %test;
        If the 'type' db is storing strings OK but the 'mtime' db is not, I give up !
        poj
Re: NDBM_File won't store data
by dash2 (Hermit) on Aug 28, 2003 at 17:16 UTC
    My only guess would be that MakeDate() returns some kind of date object rather than a string - maybe it autostringifies when you print it, but NDBM_File can't cope with it and silently fails to store it. Just a guess, I fear.
    A massive flamewar beneath your chosen depth has not been shown here

      Here is the MakeDate sub:

      sub MakeDate { my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me(time); $year = ($year + 1900) ; $mon++ ; my $date = sprintf("%04D%02d%02d", $year, $mon, $mday); return $date; }

      That should be a string right??

        Yup... so my idea wasn't right.

        I've had some problems using DBM_* in Perl before - some of them have awkward little bugs like a tendency to die if you untie the object in the right place. But what yours is, I don't know.
        A massive flamewar beneath your chosen depth has not been shown here

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://287421]
Approved by blue_cowdawg
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-16 05:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found