Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Database storage confusion

by sulfericacid (Deacon)
on Mar 08, 2003 at 21:49 UTC ( [id://241438]=perlquestion: print w/replies, xml ) Need Help??

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

I do not have any real history of using databases and I don't have any books on them. Don't suggest me to read books since I don't have the funds to get any more :( And please don't down vote this because you think it's easy or something everyone should know because guess what, I don't.

Basically all I know from databases are from example scripts I see every once in a while and I know how to store variables. That's about the extent of my knowledge.

I'm using SDBM and have run into a weird situation. I'm asking for a user's name and storing that into the DBM. All I want is one name to be stored into the database for each person. What's weird about this?

$dbm{'name'} = $name overwrites the previes entry
$dbm{$name'} = $name creates redundant Aaron Aaron
$dbm{$name}; creates Useless use of DBM error
$dbm{$name} = ""; writes Aaron

My question is, why would you need to assign a null value to $name instead of just $dbm{$nam}; ? There has to be some kind of logic because it's more writing this way and this situation must have occured with other people. You don't always need a value, what happens if all you want is a key?

As far as I can tell there is logic to only want a key sometimes, this doesn't make any sense.

Also, I am looking to store more than one variable into a database under a specific key. What would be the easiest database to use to do this? Someone already suggest MLDBM, I was just wondering if anyone else had suggestions.

IE. $name: $address|$email|$phone

Thanks for all your help everyone.



"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
•Re: Database storage confusion
by merlyn (Sage) on Mar 08, 2003 at 21:54 UTC
    A DBM is a hash with a strong will to live. It's an association of keys and values. You need to think of what part of your problem requires a persistent mapping between a key and its associated value. If your problem doesn't have that, you don't need a DBM.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Are you saying I should just write to a flatfile if all I'm trying to save is a single variable?

        Yes, that's exactly what merlyn is trying to get across. A database is pretty useless if you are only storing a single value. Much easier to simply manage a flat file and be able to read it into an array. A database becomes useful when you need to associate one piece of data with another.

        For your example, you are not associating anything with the name. All you have is a name. No age, email adress, phone number, or anything related to the user. If you did have anything, you'd then be using something like this:

        # I've never really used a dbm. # They allow complicated structures, right? $dbm{$name}{'age'} = 29; $dbm{$name}{'email'} = 'nobody@example.com';

        For a single value, I'd probably use something like this. It could use a little more work :)

        use Fcntl ':flock'; sub add_names { my @add = @_; open my $fh, '+<', 'names.dat'; flock $fh, LOCK_EX; chomp( my @names = <$fh> ); seek $fh, 0, 2; for my $name (@add) { warn "$name is already in file" and next if grep { /^$name$/i } @names; print $fh "$name\n"; } close $fh; } sub get_names { open my $fh, '<', 'names.dat'; chomp( my @names = <$fh> ); close $fh; return @names; } sub del_names { my @del = @_; open my $fh, '+<', 'names.dat'; flock $fh, LOCK_EX; chomp( my @names = <$fh> ); truncate $fh, 0; seek $fh, 0, 0; for my $name (@names) { print $fh "$name\n" unless grep { /^$name$/i } @del; } close $fh; }


        If the above content is missing any vital points or you feel that any of the information is misleading, incorrect or irrelevant, please feel free to downvote the post. At the same time, please reply to this node or /msg me to inform me as to what is wrong with the post, so that I may update the node to the best of my ability.

Re: Database storage confusion
by Cody Pendant (Prior) on Mar 09, 2003 at 00:27 UTC
    $dbm{'name'} = $name overwrites the previes entry $dbm{$name'} = $name creates redundant Aaron Aaron $dbm{$name}; creates Useless use of DBM error $dbm{$name} = ""; writes Aaron
    I don't understand a couple of those:
    $dbm{'name'} = $name overwrites the previes entry # Yes, quite correct $dbm{$name'} = $name creates redundant Aaron Aaron # I don't think so. You've got bad punctuation there, what do you expect Perl to do? Perhaps you're creating a value of "Aaron" but with a key of "Aaron'" -- see quotes? $dbm{$name}; creates Useless use of DBM error # quite so. Meaningless if that's the whole statement. $dbm{$name} = ""; writes Aaron # no it doesn't, I'm betting. $dbm{$name} should be an empty string now.

    Prove it to us with some more code?

    Like for instance:

    $name = Aaron; $dbm{$name} = $name; print $dbm{$name}; $dbm{$name'} = $name; print $dbm{$name'};
    and so on. --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
      I will post my code at the bottom of this message. What I meant with $dbm{$name} = $name; was it would hold Aaron as the key and Aaron as the value so if I were to print out the database it would print Aaron Aaron.

      The code that works for me is actually $dbm{$name} = "";, it's the only one that does what I want. You can test the script itself from what I posted below.

      #!/usr/bin/perl -W use strict; use warnings; use POSIX; use CGI; require SDBM_File; my %dbm; my $test = "test.dbm"; tie (%dbm, 'SDBM_File', $test, O_CREAT|O_RDWR, 0644) || die "Died tying database\nReason: $!\n"; print "Aaron's Phonebook System\n\n\n"; print "What is their name?\n"; my $name = $_; #if ($name ne "") { chomp($name = <STDIN>); $dbm{$name} = ""; print "Added to the list!\n";


      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        print "What is their name?\n"; my $name = $_; #if ($name ne "") { chomp($name = <STDIN>); $dbm{$name} = ""; print "Added to the list!\n";

        should read more like

        print "What is their name?\n"; chomp($name = <STDIN>); chomp($number = <STDIN>); $dbm{$name} = $number; print "$name = $dbm{$name} added to the list!\n";

        shouldn't it?


        ---
        demerphq


        Honestly, I'm more confused than ever now that you've posted that code.
        --
        “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
        M-J D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-18 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found