my $sth = $dbh->prepare ("select * from foo");
$sth->execute;
while (my $ref = $sth->fetch) {
print $ref->[0], "\n";
}
# No need for $sth->finish;
The safest way to never worry about this is to make those select loops have the smallest possible context, so the handle gets DESTROY'd on leaving scope:
{ my $sth = $dbh->prepare (...);
$sth->execute;
...
} # End-of-scope: $sth is destroyed
It is usually advisable to finish update/insert/delete handles, as they have no obvious "done" state.
{ my $stu = $dbh->prepare ("update foo set blah = ? where c_foo = ?"
+);
my $sth = $dbh->prepare ("select c_foo, bar from brimble");
$sth->execute;
while (my $r = $sth->fetchrow_hashref) {
$stu->execute ($r->{bar}, $r->{c_foo});
}
# no need for $sth->finish
$stu->finish; # Good practice
} # End of scope: clean up
Enjoy, Have FUN! H.Merijn
|