Subject: Re: How to turn $_ into a hashref?

Philip, thank you very much. I overlooked the meaning of the second variable
to selectrow_array entirely.

Two follow up questions:
1. If I don't need or want DBI handle attributes (I'm happy with the
defaults, for instance), how can I define a null hash reference?
2. I chose selectrow_array specifically because I didn't need the
prepare/execute/fetch routine. The query result is either a) the author is
already in the table, and I want the record id number, or b) the author
isn't in the table, and I'm going to add the name and then find the record
id number of the record just added. There should never be more than one
matching author name in the table, so I didn't think I needed a fetch
capable of multiple rows. You're suggesting I look at fetchrow_array(). Am I
overlooking something that you're aware of?

Thanks, again, for your help.

-Kevin

>>> "Philip M. Gollucci" <[EMAIL PROTECTED]> 05/05/05 03:38PM >>>
KEVIN ZEMBOWER wrote:

>I'm using a function from DBI that needs a hash reference according to the
documentation, and I don't know how to turn $_ into one. The section of code
I have is:
>   if ($record{"Author"}) {
>      my @indfields = split(/\|/, $record{"Author"});
>      foreach (@indfields) {
>         my $authorid = $dbh->selectrow_array("SELECT authorid FROM author
WHERE name = ?", $_)
>            or die "Can't execute statement: $DBI::errstr";
>
>
>  
>
perldoc DBI

The second argument to this is a hashref of DBI handle attributes not 
the bind values

you want:

$dbh->selectrow_array($sql, \%db_attrs, ($_));

somewhere above:
our %db_attrs = (
    RaiseError => 1,
    PrintError => 0,
    AutoCommit => 1,
    Taint      => 1
    ......
);

Also, the how point of using bind values is you should $dbh->prepare() 
your query outside of the loop.

then $sth->execute($_) in the loop.

and $sth->finish() after the loop.

Check out $sth->fetchrow_arrary() instead.

HTH

-- 
END
----------------------------------------------------------------------------

Well looking at your code ... right before the DBI call you do a
"foreach"...  That would indicate, to me, that you "expected" to "fetch"
more than one row... but if you truly expect to only fetch one row then yes,
do the selectrow_array. But if not... 

The DBI "prepare" function is "similar" to declaring a cursor in PLSql then
you only "pay once" to parse the SQL... The execute is "like" the PLSql open
of a cursor... and the fetchrow_array is "like" fetch cursor into
statement... 

I also like very much the DBI functions selectall_arrayref and
selectall_hashref function for one time small sets/subsets of data.

This is a really nice package with lots of ways to do the same thing
different ways given different circumstances. Choose what is best for you...

jwm

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to