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

Hi Monks, I was trying to open an already existing DB file. It has some data in it. but when I try to open it and print it, it wouldn't work(wouldn't enter the while loop). One of the monks suggested me to insert a record and try it. it showed that record. what about the already existing data. Is there any way by which I can retrieve that.
dbmopen(%TEST, "/home/lab/access.db", 0666); #$TEST{'test_key'} = pack 'L', int(rand 2**32); while (($key,$val) = each %TEST) { print $key, ' = ', unpack('L',$val), "\n"; print "test2"; }
if the commented line is uncommented. The output is
test_key = 2412538716 test2
if the line 2 line is not present, it shows nothing. its not entering the while loop. as if the db file is empty. but it is not. file permissions, path etc are ok. the file access.db was created by an other person, one year back, on a mandrake7 machine, using db1.x, DB_File1.808 and perl5. right now, I use db4.x, DB_File1.808 and perl5.8.1. Thanks in advance UPDATE : Hi Dave, thanks for your suggestion, I already tried that and the message in the Die is not printed. So i guess it opens the file and it doesn't find any keys, so it wouldnt enter the while loop. I donno if I'm correct. I read the link that you sent me about dbmopen, but the problem is, I already have an existing 1500 line script that uses the above methos instead of "tie". It will be a lot easier for me to get this one working than changin the entire script. Thank you

Replies are listed 'Best First'.
Re: dbmopen not opening a db file
by davido (Cardinal) on Jun 11, 2004 at 00:13 UTC
    Out of curiosity, what happens if you check to verify that the access.db file is actually successfully opening? Change that first line as follows:

    dbmopen( %TEST, "/home/lab/access.db", 0666 ) or die "Couldn't open database: $!\n";

    My first guess is that you're not successful in opening the database, and that extra "or die..." stuff will alert you to the problem.

    Also, have a look at the documentation for dbmopen, where it says that the dbmopen function has been largely superseded by tie.


    Dave

Re: dbmopen not opening a db file
by tilly (Archbishop) on Jun 13, 2004 at 01:51 UTC
    Based on your description of what is happening, DB_File really thinks that the file is empty. There are many ways in which this could have happened, including your having the wrong file, the person who created it having messed up, the result of a software conflict between the old and the current version of BerkeleyDB, the person who created it didn't have what you think was on the machine so dbmopen fell back (through AnyDBM_File) to a different dbm implementation than you think it did, the file that you think you're loading is not in the location that you're opening, etc, etc, etc. I have no way to know which of these is happening. I can only tell you that as far as dbmopen is concerned, you don't have data in the file being opened.

    As for using tie, it is a small change. Change the dbmopen line to:

    use DB_File; tie %TEST, 'DB_File', '/home/lab/access.db';
    and if there is a dbmclose line, just call untie.

    The change is tiny. The advantage is that you guarantee that you aren't creating with one dbm and then trying to read with another later. If what you've said is correct, though, it likely won't help you. But you should try to use tie and try out each of NDBM_File, SDBM_File and ODBM_file to see if it is in one of those formats. (Type perldoc X to see if X is installed and to find out how to try using it.)

    If all else fails, then chalk this up to lessons learned - always have a plaintext backup that you can restore critical data from.