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

📄 ch12_03.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>DBI Methods (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="ch12_02.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="ch12_04.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">12.3. DBI Methods</h2><p><a name="INDEX-1764" />Thefollowing methods are available under DBI.</p><a name="INDEX-1765" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>available_drivers</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">drivers</em> = DBI-&gt;available_drivers([<em class="replaceable">nowarn</em>])</pre><p><a name="INDEX-1765" />Returns a list of available drivers bysearching <tt class="literal">@INC</tt> for the presence of DBD::* modules.</p><dl><dt><i><em class="replaceable"><tt>nowarn</tt></em></i></dt><dd>A Boolean value specifying whether to suppress warnings if somedrivers are hidden by others of the same name in earlier directories.Default is false (don't suppress).</p></dd></dl></div><a name="INDEX-1766" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>bind_col</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">result</em> = $<em class="replaceable">st_handle</em>-&gt;bind_col(<em class="replaceable">col</em>, <em class="replaceable">\variable</em> [, \%<em class="replaceable">attr</em> ])</pre><p><a name="INDEX-1766" />Binds a field of a select statement to avariable, to be updated whenever the row is fetched.</p><dl><dt><i><em class="replaceable"><tt>col</tt></em></i></dt><dd>The column number to bind</p></dd><dt><i><em class="replaceable"><tt>\variable</tt></em></i></dt><dd>A reference to the variable to bind</p></dd><dt><b><tt class="literal">\%</tt><em class="replaceable">attr</em></b></dt><dd>Attributes to set for the statement handle</p></dd></dl></div><a name="INDEX-1767" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>bind_columns</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">result</em> = $<em class="replaceable">st_handle</em>-&gt;bind_columns(\%<em class="replaceable">attr</em>, <tt class="literal">@</tt><em class="replaceable">reflist</em> )</pre><p><a name="INDEX-1767" />Runs <tt class="literal">bind_col</tt> on eachcolumn of the statement.</p><dl><dt><b><tt class="literal">\%</tt><em class="replaceable">attr</em></b></dt><dd>Attributes to set for the statement handle</p></dd><dt><b><tt class="literal">@</tt><em class="replaceable">reflist</em></b></dt><dd>A list of references to the variables to bind</p></dd></dl></div><a name="INDEX-1768" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>bind_param</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">result</em> = $<em class="replaceable">st_handle</em>-&gt;bind_param(<em class="replaceable">n</em>, <em class="replaceable">value</em> [, <em class="replaceable">type</em> ])</pre><p><a name="INDEX-1768" />Binds a value with a placeholder in aprepared statement.</p><dl><dt><i><em class="replaceable"><tt>n</tt></em></i></dt><dd>The parameter number to bind.</p></dd><dt><i><em class="replaceable"><tt>value</tt></em></i></dt><dd>The value to associate with parameter <em class="replaceable"><tt>n</tt></em>.</p></dd><dt><i><em class="replaceable"><tt>type</tt></em></i></dt><dd>The data type for the placeholder. The type can also be specified asan anonymous list (<tt class="literal">{TYPE =&gt;</tt><em class="replaceable"><tt>type</tt></em><tt class="literal">}</tt>).</p></dd></dl></div><a name="INDEX-1769" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>commit</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">result</em> = $<em class="replaceable">db_handle</em>-&gt;commit</pre><p><a name="INDEX-1769" />Commits the most recent changes. See alsothe AutoCommit attribute.</p></div><a name="INDEX-1770" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>connect</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">db_handle</em> = DBI-&gt;connect(<em class="replaceable">data_source, user, passwd,</em>[\%<em class="replaceable">attr</em>])</pre><p><a name="INDEX-1770" />Connects to the specified database,returning a database handle object. The <tt class="literal">connect</tt>method automatically installs the driver if it has not already beeninstalled.</p><dl><dt><i><em class="replaceable"><tt>data_source</tt></em></i></dt><dd>A string identifying the driver to connect to, and any otheradditional driver-specific information needed to identify the driver(e.g., a port number). The driver name is written in the form<tt class="literal">dbi</tt>:<em class="replaceable"><tt>driver_name</tt></em>, e.g.,<tt class="literal">dbi:mysql</tt>. (Default value is taken from theDBI_DSN environment variable.) For example, a connection string forthe MySQL driver always starts with <tt class="literal">dbi:mysql</tt>:.The second colon should be followed by any information required bythe driver to make the database connection. In the case of MySQL, youmust supply a database name (<tt class="literal">bookdb</tt> in thefollowing example), a hostname (<tt class="literal">localhost</tt>), andthe port number of the database server (<tt class="literal">1114</tt>):</p><blockquote><pre class="code">DBI:$driver:database=$database;host=$hostname;port=$port</pre></blockquote></dd><dt><i><em class="replaceable"><tt>user</tt></em></i></dt><dd>The username for the database connection. (Default value is takenfrom the DBI_USER environment variable.)</p></dd><dt><i><em class="replaceable"><tt>passwd</tt></em></i></dt><dd>The password for the database connection. (Default value is takenfrom the DBI_PASS environment variable. Set this value at your ownrisk.)</p></dd><dt><b><tt class="literal">\%</tt><em class="replaceable">attr</em></b></dt><dd>A hash reference defining attributes for the database handle.</p></dd></dl></div><a name="INDEX-1771" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>data_sources</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">drivers</em> = DBI-&gt;data_sources(<em class="replaceable">driver</em>)</pre><p><a name="INDEX-1771" />Returns a list of all databases availablefor the named driver. (The database server must be running for<tt class="literal">data_sources</tt> to return any results.)</p><dl><dt><i><em class="replaceable"><tt>driver</tt></em></i></dt><dd>The driver to list. If unspecified, the value of DBI_DRIVER is used.</p></dd></dl></div><a name="INDEX-1772" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>disconnect</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">result</em> = $<em class="replaceable">db_handle</em>-&gt;disconnect</pre><p><a name="INDEX-1772" />Disconnects the database.</p></div><a name="INDEX-1773" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>do</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">$rows</em> = <em class="replaceable">$db_handle</em>-&gt;do(<em class="replaceable">statement</em> [, \%<em class="replaceable">attr</em>, @<em class="replaceable">bind_values</em> ])</pre><p><a name="INDEX-1773" />Prepares and executes a statement,returning the number of rows affected.</p><dl><dt><i><em class="replaceable"><tt>statement</tt></em></i></dt><dd>The statement to execute</p></dd><dt><b><tt class="literal">\%</tt><em class="replaceable">attr</em></b></dt><dd>Attributes to set for the new statement</p></dd><dt><b><tt class="literal">@</tt><em class="replaceable">bind_values</em></b></dt><dd>Bind values for placeholder substitution</p></dd></dl></div><a name="INDEX-1774" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>dump_results</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">rows</em> = DBI::dump_results(<em class="replaceable">st_handle</em>,<em class="replaceable"> maxlen</em>,<em class="replaceable"> ldelim</em>,<em class="replaceable"> fdelim</em>,<em class="replaceable"> fileh</em>)</pre><p><a name="INDEX-1774" />Runs <tt class="literal">DBI::neat( )</tt> onall the rows of a statement handle and prints them for testingpurposes.</p><dl><dt><i><em class="replaceable"><tt>st_handle</tt></em></i></dt><dd>The statement handle to retrieve.</p></dd><dt><i><em class="replaceable"><tt>maxlen</tt></em></i></dt><dd>The length at which to truncate with"...". Default is<tt class="literal">35</tt>.</p></dd><dt><i><em class="replaceable"><tt>ldelim</tt></em></i></dt><dd>The delimiter between rows. Default is <tt class="literal">\n</tt>.</p></dd><dt><i><em class="replaceable"><tt>fdelim</tt></em></i></dt><dd>The delimiter between individual fields. Default is<tt class="literal">,</tt> .</p></dd><dt><i><em class="replaceable"><tt>fileh</tt></em></i></dt><dd>The filehandle to print to. Default is <tt class="literal">STDOUT</tt>.</p></dd></dl></div>

⌨️ 快捷键说明

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