faq.html
来自「perl教程」· HTML 代码 · 共 1,069 行 · 第 1/4 页
HTML
1,069 行
hash with each of these <code>httpd</code> child. If your application is based on a
single database user, this connection can be started with each child.
Currently, database connections cannot be shared between <code>httpd</code> children.</p>
<p><em>Apache::DBI</em> can be downloaded from CPAN <em>via</em>:</p>
<pre>
<a href="http://www.perl.com/cgi-bin/cpan_mod?module=Apache::DBI">http://www.perl.com/cgi-bin/cpan_mod?module=Apache::DBI</a></pre>
<p>
</p>
<h2><a name="4_4___when_i_run_a_perl_script_from_the_command_line__it_works__but__when_i_run_it_under_the_httpd__it_fails____why">4.4 ``When I run a perl script from the command line, it works, but, when I run it under the <code>httpd</code>, it fails!'' Why?</a></h2>
<p>Basically, a good chance this is occurring is due to the fact that the user
that you ran it from the command line as has a correctly configured set of
environment variables, in the case of <em>DBD::Oracle</em>, variables like
<code>ORACLE_HOME</code>, <code>ORACLE_SID</code> or <code>TWO_TASK</code>.</p>
<p>The <code>httpd</code> process usually runs under the user id of <code>nobody</code>,
which implies there is no configured environment. Any scripts attempting to
execute in this situation will correctly fail.</p>
<p>One way to solve this problem is to set the environment for your database in a
<code>BEGIN { }</code> block at the top of your script. Another technique is to configure
your WWW server to pass-through certain environment variables to your CGI
scripts.</p>
<p>Similarly, you should check your <code>httpd</code> error logfile for any clues,
as well as the ``Idiot's Guide To Solving Perl / CGI Problems'' and
``Perl CGI Programming FAQ'' for further information. It is
unlikely the problem is DBI-related.</p>
<p>The ``Idiot's Guide To Solving Perl / CGI Problems'' can be located at:</p>
<pre>
<a href="http://www.perl.com/perl/faq/index.html">http://www.perl.com/perl/faq/index.html</a></pre>
<p>as can the ``Perl CGI Programming FAQ''. Read <em>BOTH</em> these documents
carefully!</p>
<p>
</p>
<h2><a name="4_5_how_do_i_get_the_number_of_rows_returned_from_a_select_statement">4.5 How do I get the number of rows returned from a <code>SELECT</code> statement?</a></h2>
<p>Count them. Read the DBI docs for the <code>rows()</code> method.</p>
<p>
</p>
<hr />
<h1><a name="miscellaneous_questions">Miscellaneous Questions</a></h1>
<p>
</p>
<h2><a name="5_1_can_i_do_multithreading_with_dbi">5.1 Can I do multi-threading with DBI?</a></h2>
<p>Perl version 5.005 and later can be built to support multi-threading.
The DBI, as of version 1.02, does not yet support multi-threading
so it would be unsafe to let more than one thread enter the DBI at
the same time.</p>
<p>It is expected that some future version of the DBI will at least be
thread-safe (but not thread-hot) by automatically blocking threads
intering the DBI while it's already in use.</p>
<p>For some OCI example code for Oracle that has multi-threaded <code>SELECT</code>
statements, see:</p>
<pre>
<a href="http://www.symbolstone.org/technology/oracle/oci/orathreads.tar.gz">http://www.symbolstone.org/technology/oracle/oci/orathreads.tar.gz</a></pre>
<p>
</p>
<h2><a name="5_2_how_do_i_handle_blob_data_with_dbi">5.2 How do I handle BLOB data with DBI?</a></h2>
<p>Handling BLOB data with the DBI is very straight-forward. BLOB columns are
specified in a SELECT statement as per normal columns. However, you also
need to specify a maximum BLOB size that the <I>database handle</I> can
fetch using the <code>LongReadLen</code> attribute.</p>
<p>For example:</p>
<pre>
<span class="comment">### $dbh is a connected database handle</span>
<span class="variable">$sth</span> <span class="operator">=</span> <span class="variable">$dbh</span><span class="operator">-></span><span class="variable">prepare</span><span class="operator">(</span> <span class="string">"SELECT blob_column FROM blobby_table"</span> <span class="operator">);</span>
<span class="variable">$sth</span><span class="operator">-></span><span class="variable">execute</span><span class="operator">;</span>
</pre>
<p>would fail.</p>
<pre>
<span class="comment">### $dbh is a connected database handle</span>
<span class="comment">### Set the maximum BLOB size...</span>
<span class="variable">$dbh</span><span class="operator">-></span><span class="operator">{</span><span class="string">LongReadLen</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">16384</span><span class="operator">;</span> <span class="comment">### 16Kb...Not much of a BLOB!</span>
</pre>
<pre>
<span class="variable">$sth</span> <span class="operator">=</span> <span class="variable">$dbh</span><span class="operator">-></span><span class="variable">prepare</span><span class="operator">(</span> <span class="string">"..."</span> <span class="operator">);</span>
</pre>
<p>would succeed <I>provided no column values were larger than the specified
value</I>.</p>
<p>If the BLOB data is longer than the value of <code>LongReadLen</code>, then an
error will occur. However, the DBI provides an additional piece of
functionality that will automatically truncate the fetched BLOB to the
size of <code>LongReadLen</code> if it is longer. This does not cause an error to
occur, but may make your fetched BLOB data useless.</p>
<p>This behaviour is regulated by the <code>LongTruncOk</code> attribute which is
defaultly set to a false value ( thus making overlong BLOB fetches fail ).</p>
<pre>
<span class="comment">### Set BLOB handling such that it's 16Kb and can be truncated</span>
<span class="variable">$dbh</span><span class="operator">-></span><span class="operator">{</span><span class="string">LongReadLen</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">16384</span><span class="operator">;</span>
<span class="variable">$dbh</span><span class="operator">-></span><span class="operator">{</span><span class="string">LongTruncOk</span><span class="operator">}</span> <span class="operator">=</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>Truncation of BLOB data may not be a big deal in cases where the BLOB
contains run-length encoded data, but data containing checksums at the end,
for example, a ZIP file, would be rendered useless.</p>
<pre>
=head2 5.3 How can I invoke stored procedures with DBI?</pre>
<p>The DBI does not define a database-independent way of calling stored procedures.</p>
<p>However, most database that support them also provide a way to call
them from SQL statements - and the DBI certainly supports that.</p>
<p>So, assuming that you have created a stored procedure within the target
database, <em>eg</em>, an Oracle database, you can use <code>$dbh</code>-><a href="../../lib/Pod/perlfunc.html#item_do"><code>do()</code></a> to
immediately execute the procedure. For example,</p>
<pre>
<span class="variable">$dbh</span><span class="operator">-></span><span class="keyword">do</span><span class="operator">(</span> <span class="string">"BEGIN someProcedure; END;"</span> <span class="operator">);</span> <span class="comment"># Oracle-specific</span>
</pre>
<p>You should also be able to <code>prepare</code> and <code>execute</code>, which is
the recommended way if you'll be calling the procedure often.</p>
<p>
</p>
<h2><a name="5_4_how_can_i_get_return_values_from_stored_procedures_with_dbi">5.4 How can I get return values from stored procedures with DBI?</a></h2>
<pre>
Contributed by Jeff Urlwin</pre>
<pre>
<span class="variable">$sth</span> <span class="operator">=</span> <span class="variable">$dbh</span><span class="operator">-></span><span class="variable">prepare</span><span class="operator">(</span> <span class="string">"BEGIN foo(:1, :2, :3); END;"</span> <span class="operator">);</span>
<span class="variable">$sth</span><span class="operator">-></span><span class="variable">bind_param</span><span class="operator">(</span><span class="number">1</span><span class="operator">,</span> <span class="variable">$a</span><span class="operator">);</span>
<span class="variable">$sth</span><span class="operator">-></span><span class="variable">bind_param_inout</span><span class="operator">(</span><span class="number">2</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">$path</span><span class="operator">,</span> <span class="number">2000</span><span class="operator">);</span>
<span class="variable">$sth</span><span class="operator">-></span><span class="variable">bind_param_inout</span><span class="operator">(</span><span class="number">3</span><span class="operator">,</span> <span class="operator">\</span><span class="variable">$success</span><span class="operator">,</span> <span class="number">2000</span><span class="operator">);</span>
<span class="variable">$sth</span><span class="operator">-></span><span class="variable">execute</span><span class="operator">;</span>
</pre>
<p>Remember to perform error checking, though! ( Or use the <code>RaiseError</code>
attribute ).</p>
<p>
</p>
<h2><a name="5_5_how_can_i_create_or_drop_a_database_with_dbi">5.5 How can I create or drop a database with DBI?</a></h2>
<p>Database creation and deletion are concepts that are entirely too abstract
to be adequately supported by DBI. For example, Oracle does not support the
concept of dropping a database at all! Also, in Oracle, the database
<em>server</em> essentially <em>is</em> the database, whereas in mSQL, the
server process runs happily without any databases created in it. The
problem is too disparate to attack in a worthwhile way.</p>
<p>Some drivers, therefore, support database creation and deletion through
the private <code>func()</code> methods. You should check the documentation for
the drivers you are using to see if they support this mechanism.</p>
<p>
</p>
<h2><a name="5_6_how_can_i_commit_or_rollback_a_statement_with_dbi">5.6 How can I <code>commit</code> or <code>rollback</code> a statement with DBI?</a></h2>
<p>See the <code>commit()</code> and <code>rollback()</code> methods in the DBI Specification.</p>
<p>Chapter 6 of "Programming the Perl DBI" discusses transaction handling within
the context of DBI in more detail.</p>
<p>
</p>
<h2><a name="5_7_how_are_null_values_handled_by_dbi">5.7 How are <code>NULL</code> values handled by DBI?</a></h2>
<p><code>NULL</code> values in DBI are specified to be treated as the value <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.
<code>NULL</code>s can be inserted into databases as <code>NULL</code>, for example:</p>
<pre>
<span class="variable">$rv</span> <span class="operator">=</span> <span class="variable">$dbh</span><span class="operator">-></span><span class="keyword">do</span><span class="operator">(</span> <span class="string">"INSERT INTO table VALUES( NULL )"</span> <span class="operator">);</span>
</pre>
<p>but when queried back, the <code>NULL</code>s should be tested against <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.
This is standard across all drivers.</p>
<p>
</p>
<h2><a name="5_8_what_are_these_func___methods_all_about">5.8 What are these <code>func()</code> methods all about?</a></h2>
<p>The <code>func()</code> method is defined within DBI as being an entry point
for database-specific functionality, <em>eg</em>, the ability to create or
drop databases. Invoking these driver-specific methods is simple, for example,
to invoke a <code>createDatabase</code> method that has one argument, we would
write:</p>
<pre>
<span class="variable">$rv</span> <span class="operator">=</span><span class="variable">$dbh</span><span class="operator">-></span><span class="variable">func</span><span class="operator">(</span> <span class="string">'argument'</span><span class="operator">,</span> <span class="string">'createDatabase'</span> <span class="operator">);</span>
</pre>
<p>Software developers should note that the <code>func()</code> methods are
non-portable between databases.</p>
<p>
</p>
<h2><a name="5_9_is_dbi_year_2000_compliant">5.9 Is DBI Year 2000 Compliant?</a></h2>
<p>DBI has no knowledge of understanding of what dates are. Therefore, DBI
itself does not have a Year 2000 problem. Individual drivers may use date
handling code internally and therefore be potentially susceptible to the
Year 2000 problem, but this is unlikely.</p>
<p>You may also wish to read the ``Does Perl have a Year 2000 problem?'' section
of the Perl FAQ at:</p>
<pre>
<a href="http://www.perl.com/CPAN/doc/FAQs/FAQ/PerlFAQ.html">http://www.perl.com/CPAN/doc/FAQs/FAQ/PerlFAQ.html</a></pre>
<p>
</p>
<hr />
<h1><a name="support_and_training">Support and Training</a></h1>
<p>The Perl5 Database Interface is <em>FREE</em> software. IT COMES WITHOUT WARRANTY
OF ANY KIND. See the DBI README for more details.</p>
<p>However, some organizations are providing either technical support or
training programs on DBI. The present author has no knowledge as
to the quality of these services. The links are included for reference
purposes only and should not be regarded as recommendations in any way.
<em>Caveat emptor</em>.</p>
<p>
</p>
<h2><a name="commercial_support">Commercial Support</a></h2>
<dl>
<dt><strong><a name="item_the_perl_clinic">The Perl Clinic</a></strong>
<dd>
<p>The Perl Clinic provides commercial support for <em>Perl</em> and Perl
related problems, including the <em>DBI</em> and its drivers. Support is
provided by the company with whom Tim Bunce, author of <em>DBI</em> and
<em>DBD::Oracle</em>, works and ActiveState. For more information on their
services, please see:</p>
</dd>
<dd>
<pre>
<a href="http://www.perlclinic.com">http://www.perlclinic.com</a></pre>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="training">Training</a></h2>
<dl>
<dt><strong><a name="item_westlake_solutions">Westlake Solutions</a></strong>
<dd>
<p>A hands-on class for experienced Perl CGI developers that teaches
how to write database-connected CGI scripts using Perl and DBI.pm. This
course, along with four other courses on CGI scripting with Perl, is
taught in Washington, DC; Arlington, Virginia; and on-site worldwide upon
request.</p>
</dd>
<dd>
<p>See:</p>
</dd>
<dd>
<pre>
<a href="http://www.westlake.com/training">http://www.westlake.com/training</a></pre>
</dd>
<dd>
<p>for more details.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="other_references">Other References</a></h1>
<p>In this section, we present some miscellaneous WWW links that may be of
some interest to DBI users. These are not verified and may result in
unknown sites or missing documents.</p>
<pre>
<a href="http://www-ccs.cs.umass.edu/db.html">http://www-ccs.cs.umass.edu/db.html</a>
<a href="http://www.odmg.org/odmg93/updates_dbarry.html">http://www.odmg.org/odmg93/updates_dbarry.html</a>
<a href="http://www.jcc.com/sql_stnd.html">http://www.jcc.com/sql_stnd.html</a></pre>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Alligator Descartes <<em><a href="http://www.symbolstone.org/descarte/contact.html">http://www.symbolstone.org/descarte/contact.html</a></em>>.
Portions are Copyright their original stated authors.</p>
<p>
</p>
<hr />
<h1><a name="copyright">COPYRIGHT</a></h1>
<p>This document is Copyright (c)1994-2000 Alligator Descartes, with portions
Copyright (c)1994-2000 their original authors. This module is released under
the 'Artistic' license which you can find in the perl distribution.</p>
<p>This document is Copyright (c)1997-2000 Alligator Descartes. All rights reserved.
Permission to distribute this document, in full or in part, via email,
Usenet, ftp archives or http is granted providing that no charges are involved,
reasonable attempt is made to use the most current version and all credits
and copyright notices are retained ( the <em>AUTHOR</em> and <em>COPYRIGHT</em> sections ).
Requests for other distribution rights, including incorporation into
commercial products, such as books, magazine articles or CD-ROMs should be
made to Alligator Descartes <<em><a href="http://www.symbolstone.org/descarte/contact.html">http://www.symbolstone.org/descarte/contact.html</a></em>>.</p>
<!--#include virtual="/technology/perl/DBI/templatebottom.html" -->
</BODY>
</HTML>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?