DBM Databases CGI/Perl Programming 9

advertisement
9
DBM Databases
CGI/Perl
Programming
By Diane Zak
1
9
Objectives
• In this chapter, you will:
• Create and open a DBM database
• Add a record to a DBM database
• Modify and delete a record in a
DBM database
• Determine whether a DBM
database contains a specific key
2
9
Objectives
• In this chapter, you will:
• Close a DBM database
• Concatenate strings
• Create a “here” document
3
9
Introduction
• Most businesses store
information in databases:
– Inventory
– Employee information
– Customer information
• Databases can be created with a
database software package:
–
–
–
–
Oracle
Microsoft Access
Sybase
MySQL
4
9
Introduction
• The Perl DBI module is used with
the appropriate DBD file to
manipulate the database
– DBI = Database Interface
– DBD = Database Driver
• One of the Perl DBM modules can
also be used to create and
manipulate a DBM database
– DBM = Database Management
5
9
The Jeffrey Sikes Band Form
6
Planning and Coding the
Jeffrey Sikes Band Script
9
–   is used to display
a blank space in a Web
page
7
Creating and Opening
a DBM Database
9
• Examples of Perl DBM modules:
– ODBM_File.pm
– NBDM_File.pm
– SDBM_File.pm
• Will concentrate on SDBM because it
is included in the standard Perl
distribution for Windows and UNIX
• To use the module, need to include
the line:
– use SDBM_File;
8
Creating and Opening
a DBM Database
9
• Good programming practice to plan a
database before creating it
– List the fields to include in each record
– Identify the primary key in the list
• A unique identifier of a record
• In this mailing list database – email address
• A primary key can be more than one field
• DBM modules store records on disk
using a key/value format – like a hash
– key – data entered in each record’s
primary key field
– value – data entered in the remaining
fields in the record
9
Creating and Opening
a DBM Database
9
• The tie function
is used to create
and open a DBM
database on disk
– The die function
can be used to
display a
message and exit
a script if the
tie function fails
10
Creating and Opening
a DBM Database
9
11
9
Adding and Modifying a
Record in a DBM Database
• A record is added to a DBM database
by adding a key/value pair to the
hash tied to the database
• A record is modified by modifying
the value corresponding to an
existing key in the database
• Must make changes only through the
tied hash
– Cannot make changes directly to the
DBM database
12
9
Adding and Modifying a
Record in a DBM Database
13
9
Closing a DBM Database
• Use the untie function to close
a DBM database
– Remove the tie that binds the
database to the hash
– Syntax:
• untie (hash)
– Example:
• untie (%mail)
14
9
The Jeffrey Sikes
Band Script – add function
15
9
Determining if a Specific Key
is in a DBM Database
• The exists function can be
used to determine if a specific
key is contained in a DBM
database
– Syntax:
• exists ($hash {key})
– Example:
• if (exists ($mail {$email}))
16
Deleting a Record From
a DBM Database
9
• The delete function is used to
delete a record from a DBM
database
– Syntax:
• delete ($hash {key})
– Example:
• delete ($mail {$email})
17
9
Concatenating Strings
• The concatenation operator (.)
is used to connect multiple
strings
18
9
The Jeffrey Sikes
Band Script – remove function
19
Completed band.cgi Script
9
20
Creating a
“here” Document
9
• Most CGI scripts contain many print
statements specifically to output
HTML code
• A “here” document allows you to use
one print statement for a block of
HTML code
• Syntax:
print <<label;
HTML instructions
label
21
9
Creating a “here” Document
• Example of a “here” document:
#create Web page
print <<endHtml;
<HTML>
<HEAD><TITLE>The Jeffrey Sikes
Band</TITLE></HEAD>
<BODY BGCOLOR=silver>
<FONT SIZE=5>
<H1>The Jeffrey Sikes Band</H1>
Thank you, $name. We will send the
monthly newsletter to $email.
</FONT></BODY></HTML>
endHtml
22
Creating a
“here” Document
9
• If the label of a “here”
document is multiple words, you
need to put single or double
quotation marks around label.
– If label is enclosed in single
quotation marks, variable
interpolation will not occur.
23
Summary
9
• A database is a collection of data that is stored
in a disk file and organized so that its contents
can be easily accessed, managed, and updated.
• One of the Perl DBM (Database Management)
modules can be used to create and manipulate
a DBM database:
– SDBM_File.pm, ODBM_File.pm, NDBM_File.pm.
• The data contained in a DBM database is stored
on disk using a key/value format.
• The tie function can be used to create and
open a DBM database.
– Syntax: tie (hash, module, filename, flag, mode)
• flag – O_CREAT, O_RDONLY, O_WRONLY, O_RDWR are
defined in the Fcntl module
• mode – 0666 if O_CREAT flag, otherwise 0
24
Summary
9
• The tie function can be used to create and
open a DBM database.
– Syntax: tie (hash, module, filename, flag, mode)
• flag – O_CREAT, O_RDONLY, O_WRONLY, O_RDWR are
defined in the Fcntl module
• mode – 0666 if O_CREAT flag, otherwise 0
• The die function can be used to display a
message and exit a script if tie function fails.
• To add or modify a record in a DBM database:
– Syntax: $hash{key} = value;
• key – data in record’s primary key field
• value = data contained in remaining fields in record
• The untie function can be used to close a DBM
database.
25
– Syntax: untie (hash)
Summary
9
• The SDBM_File module creates 2 files for the
database: filename.dir and filename.pag
• The exists function can be used to determine
if a key is in a DBM database.
– Syntax: exists ($hash {key})
• The delete function can be used to remove a
record from a DBM database.
– Syntax: delete ($hash {key})
• String concatenation operator is the period (.)
• A “here” document can be used to send HTML
code to the browser
26
Download