⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch08_52.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>DB_File (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_51.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch08_53.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.52. DB_File</h2><p><a name="INDEX-1351" />Ties a Perlhash to one of the Berkeley DB database types and lets you usefunctions provided in the DB API:</p><blockquote><pre class="code">[$X =] tie %hash,  "DB_File", $filename [, $flags, $mode, $DB_HASH];[$X =] tie %hash,  "DB_File", $filename, $flags, $mode, $DB_BTREE;[$X =] tie @array, "DB_File", $filename, $flags, $mode, $DB_RECNO;</pre></blockquote><p>The types are:</p><dl><dt><b><tt class="literal">$DB_HASH</tt></b></dt><dd>Stores key/data pairs in data files. Equivalent to other hashingpackages such as DBM, NDBM, ODBM, GDBM, and SDBM.</p></dd><dt><b><tt class="literal">$DB_BTREE</tt></b></dt><dd>Stores key/data pairs in a binary tree.</p></dd><dt><b><tt class="literal">$DB_RECNO</tt></b></dt><dd>Uses a record (line) number to access fixed-length andvariable-length flat text files through the same key/value-pairinterface as in <tt class="literal">$DB_HASH</tt> and<tt class="literal">$DB_BTREE</tt>.</p></dd></dl><p>After you've <tt class="literal">tie</tt> d a hash to adatabase:</p><blockquote><pre class="code">$db = tie %hash, "DB_File", "filename";</pre></blockquote><p>you can access the Berkeley DB API functions: </p><blockquote><pre class="code">$db-&gt;put($key, $value, R_NOOVERWRITE);  # Invoke the DB "put" function</pre></blockquote><p>All the functions defined in the <em class="emphasis">dbopen(3)</em>manpage are available except <tt class="literal">close</tt> and<tt class="literal">dbopen</tt> itself. The constants defined in the<em class="emphasis">dbopen</em> manpage are also available.</p><p>The following are the functions available (the comments note only thedifferences from the equivalent C function).</p><a name="INDEX-1352" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>del</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;del(<em class="replaceable">key</em>[, <em class="replaceable">flags</em>])</pre><p><a name="INDEX-1352" />Removes key/value pairs from thedatabase. <em class="replaceable"><tt>flags</tt></em> is optional.</p></div><a name="INDEX-1353" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>fd</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;fd</pre><p><a name="INDEX-1353" />Returnsa file descriptor that represents the underlying database. Nodifference from the equivalent C function.</p></div><a name="INDEX-1354" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>get</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;get(<em class="replaceable">key</em>, <em class="replaceable">value</em>[, <em class="replaceable">flags</em>])</pre><p><a name="INDEX-1354" />Retrieves data from the database by key.<em class="replaceable"><tt>flags</tt></em> is optional. The value associatedwith <em class="replaceable"><tt>key</tt></em> is returned in<em class="replaceable"><tt>value</tt></em>.</p></div><a name="INDEX-1355" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>put</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;put(<em class="replaceable">key</em>, <em class="replaceable">value</em>[, <em class="replaceable">flags</em>])</pre><p><a name="INDEX-1355" />Stores a key/value pair in thedatabase. <em class="replaceable"><tt>flags</tt></em> is optional. If<tt class="literal">R_IAFTER</tt> or <tt class="literal">R_IBEFORE</tt> is set,then <em class="replaceable"><tt>key</tt></em> is set to the record number ofthe inserted key/value pair.</p></div><a name="INDEX-1356" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>seq</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;seq(<em class="replaceable">key</em>, <em class="replaceable">value</em>[, <em class="replaceable">flags</em>])</pre><p><a name="INDEX-1356" />Returns the next sequential key/valuepair from the database. <em class="replaceable"><tt>flags</tt></em> is optional.Both <em class="replaceable"><tt>key</tt></em> and<em class="replaceable"><tt>value</tt></em> are set.</p></div><a name="INDEX-1357" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>sync</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">X</em>-&gt;sync([<em class="replaceable">flags</em>])</pre><p><a name="INDEX-1357" />Synchronizes the database by flushingany cached data to disk. <em class="replaceable"><tt>flags</tt></em> isoptional.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_51.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_53.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.51. DB</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.53. Devel::DProf</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -