Presentation is loading. Please wait.

Presentation is loading. Please wait.

9 1 DBM Databases CGI/Perl Programming By Diane Zak.

Similar presentations


Presentation on theme: "9 1 DBM Databases CGI/Perl Programming By Diane Zak."— Presentation transcript:

1 9 1 DBM Databases CGI/Perl Programming By Diane Zak

2 9 2 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

3 9 3 Objectives In this chapter, you will: Close a DBM database Concatenate strings Create a “here” document

4 9 4 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

5 9 5 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

6 9 6 The Jeffrey Sikes Band Form

7 9 7 Planning and Coding the Jeffrey Sikes Band Script – is used to display a blank space in a Web page

8 9 8 Creating and Opening a 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;

9 9 9 Creating and Opening a 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

10 9 10 Creating and Opening a 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 the tie function fails

11 9 11 Creating and Opening a DBM Database

12 9 12 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

13 9 13 Adding and Modifying a Record in a DBM Database

14 9 14 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)

15 9 15 The Jeffrey Sikes Band Script – add function

16 9 16 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}))

17 9 17 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})

18 9 18 Concatenating Strings The concatenation operator (.) is used to connect multiple strings

19 9 19 The Jeffrey Sikes Band Script – remove function

20 9 20 Completed band.cgi Script

21 9 21 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

22 9 22 Creating a “here” Document Example of a “here” document: #create Web page print <<endHtml; The Jeffrey Sikes Band The Jeffrey Sikes Band Thank you, $name. We will send the monthly newsletter to $email. endHtml

23 9 23 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.

24 9 24 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

25 9 25 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)

26 9 26 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


Download ppt "9 1 DBM Databases CGI/Perl Programming By Diane Zak."

Similar presentations


Ads by Google