<?xml version="1.0" encoding="windows-1252"?>
<node id="154443" title="Extra Information" created="2002-03-26 12:06:21" updated="2005-08-05 17:00:02">
<type id="11">
note</type>
<author id="149022">
Revelation</author>
<data>
<field name="doctext">
I'm going to go through some things that I thought were lacking in this tutorial.  They aren't necessary, but are helpful.
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Selectrow_array:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
One thing many perl coders try to do is make their code as short as possible.  One way to shorten the code is to use selectrow_array, instead of fetcrow_array.  This is helpful if you are only executing a query once, and only getting one row of data.
&lt;br&gt;
&lt;CODE&gt;
my @row = $dbh-&gt;selectrow_array(q{SELECT my_data1, my_data2 WHERE furniture_type=?},undef, $furniture_type);
&lt;/CODE&gt;
&lt;br&gt;
This is an extremely simple, and elegant way of selecting, and will be as optimized as any selectrow, that is executed without binding, as $furniture_type is the data executed.
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;RaiseError:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
I find RaiseError to be extremely helpful for mission critical queries, and the majority of queries are mission critical to a script.  Therefore it may be easyer to just set RaiseError, and stop using &lt;code&gt;or die&lt;/code&gt;.
&lt;CODE&gt;
    $dbh = DBI-&gt;connect($DBDSN, 
		       $DBUser,
		       $DBPassword,
		       {RaiseError =&gt; 1});
&lt;/CODE&gt;
&lt;br&gt;
To unset RaiseError on a query in which you want to specify something else to do, if the query doesn't work just add the code: &lt;code&gt;$dbh-&gt;{RaiseError} = 0;&lt;/code&gt;, and set raiserror back to 1, after you are done.
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Bind_columns:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
&lt;CODE&gt;
    my ($furniture_name, $price);
    chomp($furniture_type);
    $sth-&gt;execute($furniture_type) or die "Couldn't execute: '$DBI::errstr'";
    $sth-&gt;bind_col(1, \$furniture_name);
    $sth-&gt;bind_col(2, \$price);
&lt;/CODE&gt;
You don't need to bind a specific column here.  Instead you can just bind all the columns in order, with bind_columns.
&lt;br&gt;
So instead of all that code you can write:
&lt;CODE&gt;
chomp($furniture_type);
$sth-&gt;execute($furniture_type) or die "Couldn't execute: '$DBI::errstr'";
$sth-&gt;bind_columns(\my ($furniture_name, $price));
&lt;/CODE&gt;
&lt;br&gt;
This gets rid of predeclaring the variables, and using bind_col twice.
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Update:&lt;/b&gt;  Did some research yesterday and found that bind_columns is faster for *all* queries!  Strange, but it seems that bind_columns is faster than a fetchrow_arrayref, even if only one row is being caught.  This renders fetchrow_array(ref) obselete in a sense, without using bind_columns, as a few lines of code makes a query much faster, regardless of how many rows are being returned.
&lt;br&gt;
&lt;br&gt;
You may wish to check out &lt;a href="http://archive.develooper.com/dbi-dev@perl.org/msg00049.html"&gt;this&lt;/a&gt; to see the benchmarking, and query analysis that shows that.
&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Finishing Queries:&lt;/b&gt;
&lt;br&gt;
&lt;br&gt;
One thing that is rather interesting is the code:
&lt;Code&gt;$sth-&gt;finish&lt;/Code&gt;!  Most tutorials don't teach users to finish their queries, as it's not really necessary, in the majoiry of situation.  I would advise reading the finish section of perldoc DBI, as finish is pertinant in some situations.  A link to that is &lt;a href="http://www.perldoc.com/perl5.6.1/lib/DBI.html#finish"&gt;here&lt;/a&gt;, as there's no reason for me to describe what has so aptly been described by Carlos Ramirez.  It's really code optimization, but since you went into bind_col, I figured some more optimization would be helpful.
&lt;br&gt;
&lt;br&gt;
Would like to commend the writer of this tutorial on the vast amount of information!  Great job.
&lt;hr&gt;
Gyan Kapur&lt;br&gt;
gyan.kapur@rhhllp.com</field>
<field name="root_node">
7563</field>
<field name="parent_node">
7563</field>
</data>
</node>
