260 likes | 476 Views
DBM Databases. CGI/Perl Programming By Diane Zak. 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. Objectives.
E N D
DBM Databases CGI/Perl Programming By Diane Zak
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
Objectives • In this chapter, you will: • Close a DBM database • Concatenate strings • Create a “here” document
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
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
Planning and Coding the Jeffrey Sikes Band Script • is used to display a blank space in a Web page
Creating and Openinga DBM Database • 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;
Creating and Openinga DBM Database • 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
Creating and Openinga DBM Database • 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 thetie function fails
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
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)
Determining if a Specific Key is in a DBM Database • Theexists 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}))
Deleting a Record From a DBM Database • The delete function is used to delete a record from a DBM database • Syntax: • delete ($hash {key}) • Example: • delete ($mail {$email})
Concatenating Strings • The concatenation operator (.) is used to connect multiple strings
Creating a “here” Document • 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
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
Creating a “here” Document • 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.
Summary • 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
Summary • 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. • Syntax: untie (hash)
Summary • 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