main
March 14th, 2024    

CIS 2.55
Main
Files
Syllabus
Overview
Links
Homeworks

UPLOAD HOMEWORKS

Notes
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
Bayes (src)

Tests
Sample Midterm
Sample Final

Misc
Arithmetics
Fourier Mult

Notes 0013

Databases¿ Continued

There is also a simple database capability built into Perl. It is called DBM. The idea that is you link (tie, bind, etc.) a hash (or some other type) to a file, and whatever you do to that variable, is done to that file.

DBM

The general idea is that you have code like this:

dbmopen(%hash,'somefilename',0666);
# work on hash
dbmclose(%hash);

Now, whatever you do to %hash in between these lines ends up going to (or being take out of) the 'somefilename' file.

For example, borrowing from our previous database example (from other notes), we can store the person database in this type of a database:

use DBI;

$dbh = DBI->connect("DBI:mysql:perldb",
                    "perldb",   # user name
                    "perldb",   # password
                    {RaiseError => 1});

$sth = $dbh->prepare(qq{SELECT * FROM PERSON});

$sth->execute();

dbmopen(%hash,'persondata',0666);

# fetch results, once at a time.
while($arr = $sth->fetch()){
   $hash{$arr->[0]} = join(',',@{$arr});
}

dbmclose(%hash);

$sth->finish();
$dbh->disconnect();

Notice that in the example above we join, and add the scalar variable to the hash under key which is the person id.

The data is stored in database files that have a name starting with persondata. The exact name depends on the system and database driver used.

Once that this data is in our Perl database, we can retrieve it by using dbmopen and binding a hash to the file:

dbmopen(%hash,'persondata',0666);

for $key (keys %hash) {
   $r = $hash{$key};
   print "$r\n";
}

dbmclose(%hash);

The above code would display all the contents of that hash (notice that it is a fairly simple loop that goes through every key for a hash and displays the value. The output of the code looks something like this:

2,HAN,LO,1136 Cleveland Ave,East Point,GA,30344
4,BENJAMIN,CAMP,705 Dixie St,Carrollton,GA,30117
6,JAMES,SEXSON,1680 Hospital South Dr,Austell,GA,30106
8,SUZANNE,LOWRY,1001 Thornton Rd Ste 213,Lithia Springs,GA,30122
11,CHRISTINE,ZANDER,6095 Professional Pkwy,Douglasville,GA,30134
13,RHODA,ROGERS,100 Professional Pl,Carrollton,GA,30117
15,KAREN,WELDON,4586 Timber Ridge Dr Ste 140,Douglasville,GA,30135
17,WILLIAM,WEAVER,1136 Cleveland Ave,East Point,GA,30344
19,DANIEL,WILLIAMS,1128 Southpark St,Carrollton,GA,30119
20,THOMAS,VARUGHESE,3872 Highway 5,Douglasville,GA,30135
22,MUHAMMAD,UDDIN,3280 Howell Mill Rd NW,Atlanta,GA,30327
24,ANURADHA,THOPU,1700 Hospital South Dr,Austell,GA,30106
1,MILTON,WHITE,505 Fairburn Rd SW Ste 208,Atlanta,GA,30331
3,VALENCIA,BURRUSS,1001 Thornton Rd,Lithia Springs,GA,30122
5,DENISE,NAKOS,1664 Mulkey Rd,Austell,GA,30106
7,SHARON,HAYNES,705 Dallas Hwy,Villa Rica,GA,30180
9,JAMES,LAMAR,9280 Highway 5 A,Douglasville,GA,30134
10,MICHEAL,BROOKS,868 York Ave SW,Atlanta,GA,30310
12,JANE,ZELLER,109 Professional Pl,Carrollton,GA,30117
14,LISA,WALKER,870 Crestmark Dr,Lithia Springs,GA,30122
16,MONICA,WATTS,1636 Connally Dr,East Point,GA,30344
18,ROBERT,WILLIAMS,505 Fairburn Rd,Atlanta,GA,30331
21,GEORGE,VELLANI-KARAN,58 Hospital Rd,Newnan,GA,30263
23,TASSEW,TESFAYE,868 York Ave SW,Atlanta,GA,30310
25,SONYA,THOMPSON,6095 Professional Pkwy,Douglasville,GA,30134

Notice that values are not in order; they rarely are in a hash.

What is it used for?

Sometimes you don't really need a powerful database with full relational power, etc., and all you're really storing are just name to value associations. In those cases, you might benefit from using an approach described in these notes.

Note that whatever was described is just the basics (and outdated basics at that!), so you might want to read up on DBM files if you seriously intent on using them for your projects.



































© 2006, Particle