1 / 26

DBM Databases

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.

pahana
Télécharger la présentation

DBM Databases

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DBM Databases CGI/Perl Programming By Diane Zak

  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. Objectives • In this chapter, you will: • Close a DBM database • Concatenate strings • Create a “here” document

  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. 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. The Jeffrey Sikes Band Form

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

  8. 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;

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

  10. 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

  11. Creating and Openinga DBM Database

  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. Adding and Modifying a Record in a DBM Database

  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. The Jeffrey Sikes Band Script – add function

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

  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. Concatenating Strings • The concatenation operator (.) is used to connect multiple strings

  19. The Jeffrey Sikes Band Script– remove function

  20. Completed band.cgi Script

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

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

More Related