📄 ch14_01.htm
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:42:41Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch13_16.htm"TITLE="13.15. Creating Magic Variables with tie"><LINKREL="next"HREF="ch14_02.htm"TITLE="14.1. Making and Using a DBM File"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch13_16.htm"TITLE="13.15. Creating Magic Variables with tie"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 13.15. Creating Magic Variables with tie"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_02.htm"TITLE="14.1. Making and Using a DBM File"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.1. Making and Using a DBM File"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch14-20843">14. Database Access</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch14-10943"TITLE="14.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch14_02.htm"TITLE="14.1. Making and Using a DBM File">Making and Using a DBM File</A><BR><ACLASS="sect1"HREF="ch14_03.htm"TITLE="14.2. Emptying a DBM File">Emptying a DBM File</A><BR><ACLASS="sect1"HREF="ch14_04.htm"TITLE="14.3. Converting Between DBM Files">Converting Between DBM Files</A><BR><ACLASS="sect1"HREF="ch14_05.htm"TITLE="14.4. Merging DBM Files">Merging DBM Files</A><BR><ACLASS="sect1"HREF="ch14_06.htm"TITLE="14.5. Locking DBM Files">Locking DBM Files</A><BR><ACLASS="sect1"HREF="ch14_07.htm"TITLE="14.6. Sorting Large DBM Files">Sorting Large DBM Files</A><BR><ACLASS="sect1"HREF="ch14_08.htm"TITLE="14.7. Treating a Text File as a Database Array">Treating a Text File as a Database Array</A><BR><ACLASS="sect1"HREF="ch14_09.htm"TITLE="14.8. Storing Complex Data in a DBM File">Storing Complex Data in a DBM File</A><BR><ACLASS="sect1"HREF="ch14_10.htm"TITLE="14.9. Persistent Data">Persistent Data</A><BR><ACLASS="sect1"HREF="ch14_11.htm"TITLE="14.10. Executing an SQL Command Using DBI and DBD">Executing an SQL Command Using DBI and DBD</A><BR><ACLASS="sect1"HREF="ch14_12.htm"TITLE="14.11. Program: ggh - Grep Netscape Global History">Program: ggh - Grep Netscape Global History</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>I only ask for information.</I></P><PCLASS="attribution"ALIGN="right">- Charles Dickens <CITECLASS="citetitle">David Copperfield</CITE></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-10943">14.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch14-idx-1000004908-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004908-1"></A>Everywhere you find data, you find databases. At the simplest level, every file can be considered a database. At the most complex level, expensive and complex relational database systems handle thousands of transactions per second. In between are countless improvised schemes for fast access to loosely structured data. Perl can work with all of them.</P><PCLASS="para">Early in the history of computers, people noticed that flat file databases don't scale to large data sets. Flat files were tamed using fixed-length records or auxiliary indices, but updating became expensive, and previously simple applications bogged down with I/O overhead.</P><PCLASS="para">After some head-scratching, clever programmers devised a better solution. As hashes in memory provide more flexible access to data than do arrays, hashes on disk offer more convenient kinds of access than array-like text files. These benefits in access time cost you space, but disk space is cheap these days (or so the reasoning goes).</P><PCLASS="para">The <ACLASS="indexterm"NAME="ch14-idx-1000004910-0"></A>DBM library gives Perl programmers a simple, easy-to-use database. You use the same standard operations on hashes bound to DBM files as you do on hashes in memory. In fact, that's how you use DBM databases from Perl. You call <CODECLASS="literal">dbmopen</CODE> with the name of a hash and the filename holding the database. Then whenever you access the hash, Perl consults or changes the DBM database on disk.</P><PCLASS="para"><ACLASS="xref"HREF="ch14_02.htm"TITLE="Making and Using a DBM File">Recipe 14.1</A> shows how to create a DBM database and gives tips on using it efficiently. Although you can do with DBM files the same things you do with regular hashes, their disk-based nature leads to performance concerns that don't exist with in-memory hashes. Recipes <ACLASS="xref"HREF="ch14_03.htm"TITLE="Emptying a DBM File">Recipe 14.2</A> and <ACLASS="xref"HREF="ch14_05.htm"TITLE="Merging DBM Files">Recipe 14.4</A> explain these concerns and show how to work around them. DBM files also make possible operations that aren't available using regular hashes. Recipes <ACLASS="xref"HREF="ch14_07.htm"TITLE="Sorting Large DBM Files">Recipe 14.6</A> and <ACLASS="xref"HREF="ch14_08.htm"TITLE="Treating a Text File as a Database Array">Recipe 14.7</A> explain two of these things.</P><PCLASS="para">Various DBM implementations offer varying features. The old <CODECLASS="literal">dbmopen</CODE><ACLASS="indexterm"NAME="ch14-idx-1000004911-0"></A> function only lets you use the DBM library Perl was built with. If you wanted to use <CODECLASS="literal">dbmopen</CODE> to access from one type of database and write to another, you were out of luck. Version 5 of Perl remedied this by letting you <CODECLASS="literal">tie</CODE> a hash to an arbitrary object class, as detailed in <ACLASS="xref"HREF="ch13_01.htm"TITLE="Classes, Objects, and Ties">Chapter 13, <CITECLASS="chapter">Classes, Objects, and Ties</CITE></A>.</P><PCLASS="para">The table below shows several possible DBM libraries you can choose from:</P><TABLECLASS="informaltable"BORDER="1"CELLPADDING="3"><THEADCLASS="thead"><TRCLASS="row"VALIGN="TOP"><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Feature</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">NDBM</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">SDBM</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">GDBM</P></TH><THCLASS="entry"ALIGN="LEFT"ROWSPAN="1"COLSPAN="1"><PCLASS="para">DB</P></TH></TR></THEAD><TBODYCLASS="tbody"><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Linkage comes with Perl</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Source bundled with Perl</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Source redistributable</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">gpl[<ACLASS="footnote"HREF="#ch14-pgfId-1000004358">1</A>]</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">FTPable</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Easy to build</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">N/A</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">ok[<ACLASS="footnote"HREF="#ch14-pgfId-1000004363">2</A>]</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Often comes with Unix</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes[<ACLASS="footnote"HREF="#ch14-pgfId-1000004367">3</A>]</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no[<ACLASS="footnote"HREF="#ch14-pgfId-1000004372">4</A>]</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">no[<ACLASS="footnote"HREF="#ch14-pgfId-1000004372">4</A>]</P></TD></TR><TRCLASS="row"VALIGN="TOP"><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">Builds ok on Unix</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">N/A</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes</P></TD><TDCLASS="entry"ROWSPAN="1"COLSPAN="1"><PCLASS="para">yes[<ACLASS="footnote"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -