📄 ch08.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Without a title - Title</TITLE>
</HEAD>
<BODY BACKGROUND="r2harch.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/r2harch.gif" TEXT="#000000" BGCOLOR="#FFFFFF">
<H2 ALIGN="CENTER"><A HREF="ch07.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch07.htm"><IMG SRC="blanprev.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blanprev.gif" WIDTH="37" HEIGHT="37"
ALIGN="BOTTOM" BORDER="2"></A><A HREF="index-1.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/index-1.htm"><IMG SRC="blantoc.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blantoc.gif" WIDTH="42"
HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><A HREF="ch09.htm" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/ch09.htm"><IMG SRC="blannext.gif" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/blannext.gif"
WIDTH="45" HEIGHT="37" ALIGN="BOTTOM" BORDER="2"></A><BR>
<BR>
<FONT COLOR="#0000AA">8</FONT><BR>
<A NAME="Heading1"></A><FONT COLOR="#000077">Database Access<BR>
</FONT>
<HR>
</H2>
<UL>
<LI><A HREF="#Heading1">Database Access</A>
<UL>
<LI><A HREF="#Heading3">DBI</A>
<LI><A HREF="#Heading4">Using DBI to Solve a Real World Problem</A>
<LI><A HREF="#Heading5">Listing 8.1. Records to be imported into database</A>
<UL>
<LI><A HREF="#Heading7">Importing Data into a Database with DBI</A>
</UL>
<LI><A HREF="#Heading8">Listing 8.2. Program that imports data into an mSQL database</A>
<UL>
<LI><A HREF="#Heading10">Create an HTML Index from a Database</A>
</UL>
<LI><A HREF="#Heading11">Listing 8.3. Program that writes HTML from the database</A>
<UL>
<LI><A HREF="#Heading13">Build HTML from Data on a Single Item in the Database</A>
</UL>
<LI><A HREF="#Heading14">Listing 8.4. Program that writes HTML from data</A>
<LI><A HREF="#Heading15">about a single item in the database.</A>
<UL>
<LI><A HREF="#Heading16">Searching the Database for Keywords and Outputting Results</A>
</UL>
<LI><A HREF="#Heading17">Listing 8.5. Searching the database for keywords</A>
<LI><A HREF="#Heading19">Open Database Connectivity (ODBC)</A>
<LI><A HREF="#Heading20">Summary</A>
</UL>
</UL>
<P>
<HR>
</P>
<UL>
<LI>DBI
<P>
<LI>Using DBI to Solve a Real World Problem
<P>
<LI>Open Database Connectivity (ODBC)
</UL>
<P>As the Web makes inroads into corporate intranets, one of the primary uses of
Web technology is providing an interface to data stored in corporate databases. One
of the strengths of the Web in these environments lies in its ability to provide
a platform independent, open interface to these corporate databases. Database interfaces
also play a key role in online shopping, automated order processing, and information
gathering and distribution applications. In all of these applications, using a professional
UNIX-based database package such as Oracle, Sybase, or Informix (to name a few) is
almost always the best alternative. These packages provide a powerful, configurable,
and expandable solution. Unfortunately, manufacturers of these professional database
packages have developed differing proprietary interfaces for their databases. Obviously,
this makes life difficult for programmers who want to write their own Web-based database
front ends to access data from these various databases.</P>
<P>In this chapter, we will look at how Perl5 and a set of modules provide a very
elegant solution to this problem. You will be introduced to a set of Perl5 modules
called DBI that provide a consistent database interface (API) independent of the
actual database being used. We will also look at Microsoft's Open Database Connectivity
Programming Interface, or ODBC API. ODBC is quickly being adopted by most, if not
all, major database server vendors as the standard database interface. Once all database
vendors add the ODBC interface to their previously proprietary interfaces, solutions
like DBI will not be necessary.</P>
<P>Using Perl as a database interface, or front end, through the Web has many advantages.
This approach is very flexible (there are few or no constraints on what you can do)
and very easy to experiment with.
<DL>
<DT></DT>
</DL>
<H3 ALIGN="CENTER">
<HR WIDTH="84%">
<BR>
<FONT COLOR="#000077">NOTE:</FONT></H3>
<BLOCKQUOTE>
<P>Is investing in a professional database package really worth it? Absolutely. With
standard APIs such as DBI and ODBC emerging, investing your time developing your
application to utilize a professional database makes sense. Portability from database
to database has never been easier. Investing your resources in a professional database
also makes the transition from a small enterprise to a large enterprise easier. Take,
for example, a small ISP building an online catalog. In a matter of months, that
ISP could go from taking 100 to 100,000 hits a day. If the ISP initially developed
its online catalog on an entry-level UNIX-based Silicon Graphics server using an
Oracle database, the migration to a faster SGI could be made in a matter of hours.
However, if the ISP chose to make a smaller initial investment by developing their
catalog on an Intel-based Linux or NT server, hacking together their own database,
scalability of the software and hardware architecture becomes a serious issue.<BR>
<HR>
</BLOCKQUOTE>
<H3 ALIGN="CENTER"><A NAME="Heading3"></A><FONT COLOR="#000077">DBI</FONT></H3>
<P>The DBI:: module is a Perl5 module written by Tim Bunce that provides a common
Perl interface to various database engines. DBI will allow your Perl CGI script to
manipulate data contained in a database without regard for the actual database engine
being used to service the requests. In other words, once you understand the syntax
of the different DBI functions used to access a <BR>
database, those same functions allow you to access data from an Oracle, Sybase, Informix,
or <BR>
whatever database precisely the same way, even though the different database engines
have dif-<BR>
ferent 3GL API layers. The API defined by DBI will work on all supported database
types.</P>
<P>A good example of this is a Web-based customer support application. Using DBI,
your CGI could access customer information in an Oracle database, product information
in an Informix database, and order status information in a Sybase database. DBI provides
a single standard API that allows you to access this information from all of these
databases from within the same CGI script.</P>
<P>DBI can be obtained from the Comprehensive Perl Archive Network (CPAN) at</P>
<PRE><A HREF="javascript:if(confirm('http://www.perl.com/CPAN/modules/dbperl \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.perl.com/CPAN/modules/dbperl'" tppabs="http://www.perl.com/CPAN/modules/dbperl"><FONT COLOR="#0066FF">http://www.perl.com/CPAN/modules/dbperl</FONT></A><FONT
COLOR="#0066FF">
</FONT></PRE>
<P>or its source at</P>
<PRE><A HREF="javascript:if(confirm('ftp://ftp.demon.co.uk/pub/perl/db/DBI \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.demon.co.uk/pub/perl/db/DBI'" tppabs="ftp://ftp.demon.co.uk/pub/perl/db/DBI"><FONT COLOR="#0066FF">ftp://ftp.demon.co.uk/pub/perl/db/DBI</FONT></A><FONT
COLOR="#0066FF">
</FONT></PRE>
<P>DBI implements support for the individual database engines through driver modules
called DBDs. DBDs are currently available for most major database engines. Table
8.1 lists the current DBD interfaces for DBI as of this writing. The latest information
on currently available modules is kept at the following URL:</P>
<PRE><A HREF="javascript:if(confirm('http://www.hermetica.com/technologia/DBI/DBD/index.html \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.hermetica.com/technologia/DBI/DBD/index.html'" tppabs="http://www.hermetica.com/technologia/DBI/DBD/index.html"><FONT COLOR="#0066FF">http://www.hermetica.com/technologia/DBI/DBD/index.html</FONT></A><FONT
COLOR="#0066FF">
</FONT></PRE>
<P>The DBD module for Oracle can be obtained from CPAN or its source at</P>
<PRE><A HREF="javascript:if(confirm('ftp://ftp.demon.co.uk/pub/perl/db/DBD/Oracle \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='ftp://ftp.demon.co.uk/pub/perl/db/DBD/Oracle'" tppabs="ftp://ftp.demon.co.uk/pub/perl/db/DBD/Oracle"><FONT COLOR="#0066FF">ftp://ftp.demon.co.uk/pub/perl/db/DBD/Oracle</FONT></A><FONT
COLOR="#0066FF">
</FONT></PRE>
<P><B>Table 8.1. DBD interfaces available for DBI<TT>.</TT></B><TT> </TT>
<TABLE BORDER="0">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Module Name</B></TD>
<TD ALIGN="LEFT"><B>Database Required</B></TD>
<TD ALIGN="LEFT"><B>Author</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Oracle</TD>
<TD ALIGN="LEFT">Oracle 6 or 7</TD>
<TD ALIGN="LEFT">Tim Bruce</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Sybase</TD>
<TD ALIGN="LEFT">Sybase</TD>
<TD ALIGN="LEFT">Michael Peppler</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Informix</TD>
<TD ALIGN="LEFT">Informix</TD>
<TD ALIGN="LEFT">Jonathan Leffler</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::mSQL</TD>
<TD ALIGN="LEFT">mSQL 1.0.7+</TD>
<TD ALIGN="LEFT">Alligator Descartes</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Empress</TD>
<TD ALIGN="LEFT">Empress 6.8</TD>
<TD ALIGN="LEFT">Francis Jones</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Fulcrum</TD>
<TD ALIGN="LEFT">Fulcrum SearchServer 2/3</TD>
<TD ALIGN="LEFT">Davide Migliavacca</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD:DB2</TD>
<TD ALIGN="LEFT">DB2 2.1+</TD>
<TD ALIGN="LEFT">Mike Moran</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Interbase</TD>
<TD ALIGN="LEFT">Interbase</TD>
<TD ALIGN="LEFT">Bill Karwin</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Quickbase</TD>
<TD ALIGN="LEFT">NeXTStep Quickbase</TD>
<TD ALIGN="LEFT">Ben Lindstrom</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT">DBD::Ingres</TD>
<TD ALIGN="LEFT">Ingres</TD>
<TD ALIGN="LEFT">Henrik Tougaard</TD>
</TR>
</TABLE>
<CENTER>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Using DBI to Solve a Real World
Problem</FONT></H3>
</CENTER>
<P>Thomas E. White, a computer systems analyst at Mississippi State University, put
together a nice example using DBI to set up a WWW "browse and search" application.
In this example, DBI is used with DBD::mSQL to maintain, view, browse, and search
an A/V library database.</P>
<P>The following software was configured on a Sun server for this example: Solaris
2.5</P>
<P>MiniSQL version 1.0.16</P>
<P>Perl related software</P>
<P>Version 5.003</P>
<P>CGI.pm-2.21</P>
<P>DBI.pm-0.71</P>
<P>DBD-mSQL-0.60pl10 First, the following raw records were saved to a text file,
as shown in Listing 8.1.
<CENTER>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Listing 8.1. Records to be imported
into database</FONT></H3>
</CENTER>
<PRE><FONT COLOR="#0066FF">ABC OF GOLF GREEN CONSTRUCTION
F0A4-0001.00
Film
U.S. Golf Association, Clemson University, 1970
1 Reel. 16mm. Sound. Color. 20 min.
A detailed, step-by-step process of golf green construction. Includes
planning and development,
preparation of grade, drainage and soil mixtures. Very good film.
.
College
Adult
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -