📄 faq.pm
字号:
also the amount of resource each Oracle connection will consume. I<mSQL>is lighter resource-wise and faster.These views are not necessarily representative of anyone else's opinions,and do not reflect any corporate sponsorship or views. They are providedI<as-is>.=head2 3.8 Is <I<insert feature here>> supported in DBI?Given that we're making the assumption that the feature you have requestedis a non-standard database-specific feature, then the answer will be I<no>.DBI reflects a I<generic> API that will work for most databases, and hasno database-specific functionality.However, driver authors may, if they so desire, include hooks to database-specificfunctionality through the C<func()> method defined in the DBI API.Script developers should note that use of functionality provided I<via>the C<func()> methods is very unlikely to be portable across databases.=head1 Programming Questions=head2 4.1 Is DBI any use for CGI programming?In a word, yes! DBI is hugely useful for CGI programming! In fact, I wouldtentatively say that CGI programming is one of two top uses for DBI.DBI confers the ability to CGI programmers to power WWW-fronted databasesto their users, which provides users with vast quantities of ordereddata to play with. DBI also provides the possibility that, if a site isreceiving far too much traffic than their database server can cope with, theycan upgrade the database server behind the scenes with no alterations tothe CGI scripts.=head2 4.2 How do I get faster connection times with DBD::Oracle and CGI? Contributed by John D. GroenveldThe Apache C<httpd> maintains a pool of C<httpd> children to service client requests.Using the Apache I<mod_perl> module by I<Doug MacEachern>, the perl interpreter is embedded with the C<httpd> children. The CGI, DBI, and your other favorite modules can be loaded at the startup of each child. These modules will not be reloaded unless changed on disk.For more information on Apache, see the Apache Project's WWW site: http://www.apache.orgThe I<mod_perl> module can be downloaded from CPAN I<via>: http://www.perl.com/cgi-bin/cpan_mod?module=Apache=head2 4.3 How do I get persistent connections with DBI and CGI? Contributed by John D. GroenveldUsing Edmund Mergl's I<Apache::DBI> module, database logins are stored in a hash with each of these C<httpd> 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 C<httpd> children.I<Apache::DBI> can be downloaded from CPAN I<via>: http://www.perl.com/cgi-bin/cpan_mod?module=Apache::DBI=head2 4.4 ``When I run a perl script from the command line, it works, but, when I run it under the C<httpd>, it fails!'' Why?Basically, a good chance this is occurring is due to the fact that the userthat you ran it from the command line as has a correctly configured set ofenvironment variables, in the case of I<DBD::Oracle>, variables likeC<ORACLE_HOME>, C<ORACLE_SID> or C<TWO_TASK>.The C<httpd> process usually runs under the user id of C<nobody>,which implies there is no configured environment. Any scripts attempting toexecute in this situation will correctly fail.One way to solve this problem is to set the environment for your database in aC<BEGIN { }> block at the top of your script. Another technique is to configureyour WWW server to pass-through certain environment variables to your CGI scripts.Similarly, you should check your C<httpd> 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 isunlikely the problem is DBI-related.The ``Idiot's Guide To Solving Perl / CGI Problems'' can be located at: http://www.perl.com/perl/faq/index.htmlas can the ``Perl CGI Programming FAQ''. Read I<BOTH> these documents carefully!=head2 4.5 How do I get the number of rows returned from a C<SELECT> statement?Count them. Read the DBI docs for the C<rows()> method.=head1 Miscellaneous Questions=head2 5.1 Can I do multi-threading with DBI?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-threadingso it would be unsafe to let more than one thread enter the DBI atthe same time.It is expected that some future version of the DBI will at least bethread-safe (but not thread-hot) by automatically blocking threadsintering the DBI while it's already in use.For some OCI example code for Oracle that has multi-threaded C<SELECT>statements, see: http://www.symbolstone.org/technology/oracle/oci/orathreads.tar.gz=head2 5.2 How do I handle BLOB data with DBI?Handling BLOB data with the DBI is very straight-forward. BLOB columns arespecified in a SELECT statement as per normal columns. However, you alsoneed to specify a maximum BLOB size that the <I>database handle</I> canfetch using the C<LongReadLen> attribute.For example: ### $dbh is a connected database handle $sth = $dbh->prepare( "SELECT blob_column FROM blobby_table" ); $sth->execute;would fail. ### $dbh is a connected database handle ### Set the maximum BLOB size... $dbh->{LongReadLen} = 16384; ### 16Kb...Not much of a BLOB! $sth = $dbh->prepare( "..." );would succeed <I>provided no column values were larger than the specifiedvalue</I>.If the BLOB data is longer than the value of C<LongReadLen>, then anerror will occur. However, the DBI provides an additional piece offunctionality that will automatically truncate the fetched BLOB to thesize of C<LongReadLen> if it is longer. This does not cause an error tooccur, but may make your fetched BLOB data useless.This behaviour is regulated by the C<LongTruncOk> attribute which is defaultly set to a false value ( thus making overlong BLOB fetches fail ). ### Set BLOB handling such that it's 16Kb and can be truncated $dbh->{LongReadLen} = 16384; $dbh->{LongTruncOk} = 1;Truncation of BLOB data may not be a big deal in cases where the BLOBcontains run-length encoded data, but data containing checksums at the end,for example, a ZIP file, would be rendered useless.=head2 5.3 How can I invoke stored procedures with DBI?The DBI does not define a database-independent way of calling stored procedures.However, most database that support them also provide a way to callthem from SQL statements - and the DBI certainly supports that.So, assuming that you have created a stored procedure within the targetdatabase, I<eg>, an Oracle database, you can use C<$dbh>->C<do()> toimmediately execute the procedure. For example, $dbh->do( "BEGIN someProcedure; END;" ); # Oracle-specificYou should also be able to C<prepare> and C<execute>, which isthe recommended way if you'll be calling the procedure often.=head2 5.4 How can I get return values from stored procedures with DBI? Contributed by Jeff Urlwin $sth = $dbh->prepare( "BEGIN foo(:1, :2, :3); END;" ); $sth->bind_param(1, $a); $sth->bind_param_inout(2, \$path, 2000); $sth->bind_param_inout(3, \$success, 2000); $sth->execute;Remember to perform error checking, though! ( Or use the C<RaiseError>attribute ).=head2 5.5 How can I create or drop a database with DBI?Database creation and deletion are concepts that are entirely too abstractto be adequately supported by DBI. For example, Oracle does not support theconcept of dropping a database at all! Also, in Oracle, the databaseI<server> essentially I<is> the database, whereas in mSQL, theserver process runs happily without any databases created in it. Theproblem is too disparate to attack in a worthwhile way.Some drivers, therefore, support database creation and deletion throughthe private C<func()> methods. You should check the documentation forthe drivers you are using to see if they support this mechanism.=head2 5.6 How can I C<commit> or C<rollback> a statement with DBI?See the C<commit()> and C<rollback()> methods in the DBI Specification.Chapter 6 of "Programming the Perl DBI" discusses transaction handling withinthe context of DBI in more detail.=head2 5.7 How are C<NULL> values handled by DBI?C<NULL> values in DBI are specified to be treated as the value C<undef>.C<NULL>s can be inserted into databases as C<NULL>, for example: $rv = $dbh->do( "INSERT INTO table VALUES( NULL )" );but when queried back, the C<NULL>s should be tested against C<undef>.This is standard across all drivers.=head2 5.8 What are these C<func()> methods all about?The C<func()> method is defined within DBI as being an entry pointfor database-specific functionality, I<eg>, the ability to create ordrop databases. Invoking these driver-specific methods is simple, for example,to invoke a C<createDatabase> method that has one argument, we wouldwrite: $rv =$dbh->func( 'argument', 'createDatabase' );Software developers should note that the C<func()> methods arenon-portable between databases.=head2 5.9 Is DBI Year 2000 Compliant?DBI has no knowledge of understanding of what dates are. Therefore, DBIitself does not have a Year 2000 problem. Individual drivers may use datehandling code internally and therefore be potentially susceptible to theYear 2000 problem, but this is unlikely.You may also wish to read the ``Does Perl have a Year 2000 problem?'' sectionof the Perl FAQ at: http://www.perl.com/CPAN/doc/FAQs/FAQ/PerlFAQ.html=head1 Support and TrainingThe Perl5 Database Interface is I<FREE> software. IT COMES WITHOUT WARRANTYOF ANY KIND. See the DBI README for more details.However, some organizations are providing either technical support ortraining programs on DBI. The present author has no knowledge asto the quality of these services. The links are included for referencepurposes only and should not be regarded as recommendations in any way.I<Caveat emptor>.=head2 Commercial Support=over 4=item The Perl ClinicThe Perl Clinic provides commercial support for I<Perl> and Perlrelated problems, including the I<DBI> and its drivers. Support isprovided by the company with whom Tim Bunce, author of I<DBI> andI<DBD::Oracle>, works and ActiveState. For more information on theirservices, please see: http://www.perlclinic.com=back=head2 Training=over 4=item Westlake SolutionsA hands-on class for experienced Perl CGI developers that teacheshow to write database-connected CGI scripts using Perl and DBI.pm. Thiscourse, along with four other courses on CGI scripting with Perl, istaught in Washington, DC; Arlington, Virginia; and on-site worldwide uponrequest.See: http://www.westlake.com/trainingfor more details.=back=head1 Other ReferencesIn this section, we present some miscellaneous WWW links that may be ofsome interest to DBI users. These are not verified and may result inunknown sites or missing documents. http://www-ccs.cs.umass.edu/db.html http://www.odmg.org/odmg93/updates_dbarry.html http://www.jcc.com/sql_stnd.html=head1 AUTHORAlligator Descartes <I<http://www.symbolstone.org/descarte/contact.html>>. Portions are Copyright their original stated authors.=head1 COPYRIGHTThis document is Copyright (c)1994-2000 Alligator Descartes, with portionsCopyright (c)1994-2000 their original authors. This module is released underthe 'Artistic' license which you can find in the perl distribution.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 creditsand copyright notices are retained ( the I<AUTHOR> and I<COPYRIGHT> sections ).Requests for other distribution rights, including incorporation into commercial products, such as books, magazine articles or CD-ROMs should bemade to Alligator Descartes <I<http://www.symbolstone.org/descarte/contact.html>>.=for html<!--#include virtual="/technology/perl/DBI/templatebottom.html" --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -