Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Getting started with databases

by zengargoyle (Deacon)
on Oct 10, 2003 at 13:40 UTC ( #298253=note: print w/replies, xml ) Need Help??


in reply to Getting started with databases

read the perldocs for the modules. these are nothing more than persistant (saved to disk) versions of the familiar hash with some extra restrictions.

quoth perdoc AnyDBM:


       DBM Comparisons

       Here's a partial table of features the different packages offer:

                                odbm    ndbm    sdbm    gdbm    bsd-db
                                ----    ----    ----    ----    ------
        Linkage comes w/ perl   yes     yes     yes     yes     yes
        Src comes w/ perl       no      no      yes     no      no
        Comes w/ many unix os   yes     yes  no      no      no
        Builds ok on !unix      ?       ?       yes     yes     ?
        Code Size               ?       ?       small   big     big
        Database Size           ?       ?       small   big?    ok
        Speed                   ?       ?       slow    ok      fast
        FTPable                 no      no      yes     yes     yes
        Easy to build          N/A     N/A      yes     yes     ok
        Size limits             1k      4k      1k   none    none
        Byte-order independent  no      no      no      no      yes
        Licensing restrictions  ?       ?       no      yes     no
the different dbm modules all work about the same, just have different file formats and limitations. AnyDBM is a wrapper to hide those differences from the programmer, it will let you use the same code to open an NDBM or SDBM or ODBM format file.

use Fcntl; # For O_RDWR, O_CREAT, etc. use SDBM_File; tie(%h, 'SDBM_File', 'filename', O_RDWR|O_CREAT, 0666) or die "Couldn't tie SDBM file 'filename': $!; aborting"; # Now read and change the hash $h{count} += 1; print $h{count}; ... untie %h;

each time you run the code %h will be just like it was and you should see the counter go up.

DBM stores key => value pairs, the value has to be a scalar so:

$h{user} = "bob";
will work,
$h{prefs} = { color => 'red', font => 'large' };
won't work. there's a MLDBM module that will help you accomplish the second, or you can use Data::Dumper or YAML or FreezeThaw to convert your data structure into a string (that you store in the DBM, fetch later and unpack before using).

so a DBM is a simple key => value store that you can search/fetch by the key only. a RDBMS/SQL/Real Database is a lot like a DBM with multiple values, and every value can be a key. the data is arranged in tuples (think ARRAY) but they're regular and have names for each position so you can think of them as HASHes. there are multiple tuples per table and multiple tables per database and multiple databases per server ...

server: foo database: stuff table: users |name|age|sex|location| |bob|12|M|Nuke York| |mary|22|F|Lost Angles| table: prefs |name|color|font| |bob|blue|large| |mary|blue|tiny|

now you query with SQL which is almost understandable...

SELECT name FROM prefs WHERE font = 'tiny'; |name| |mary| SELECT name, color, sex FROM users, prefs WHERE color = 'blue'; |name|color|sex| |bob|blue|M| |mary|blue|F|
you would likely get that second bit back in perl as something like:
$result => [ { name => 'bob', color => 'blue', sex => 'M' }, { name => 'mary', color => 'blue', sex => 'F' } ];

if the DBM provides enough funcionality for your purpose use it. if not install a simple SQL database like SQLite and work through the examples. then go an read some of the fine tutorials and/or books people are sure to mention.

Replies are listed 'Best First'.
Re: Re: Getting started with databases
by Anonymous Monk on Oct 10, 2003 at 13:58 UTC

    Do you think this blurb is going to make sense to the OP, who clearly states being an absolute database beginner?

      yes, i think it will make sense to katgirl. katgirl has a 'site' and currently stores data in a flat-file. that implies at least a bit of knowledge about CGI, reading/writing files, and storage of persistant data. definately above the threshold for understanding that a magical 'tie' can link a HASH to a file automatically.

      not only that, but there wasn't another response that actually answered the OP question as to what those DBM modules that are already installed actually do. database class this, read these books, plan requirements, ... made everything sound so hard.

      though i will admit to answering the question because katgirl is of the feline persuasion.

      I think so, yes. zengargoyle says these are nothing more than persistant (saved to disk) versions of the familiar hash with some extra restrictions, and I know for a fact katgirl knows what a hash is :)

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://298253]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2019-10-19 04:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?