in reply to Copy an object

Unless this is something you do only once in your program, you shouldn't reconnect to the database every time you need to do a select.
Instead, you should connect once and then use the database handle throughout your program; e.g., you would pass it to all subroutines that work with the DB.
# example my $dbh = DBI->connect( ... ); #... my @row = doSelect($dbh, 'foobar'); #... sub doSelect{ my ($dbh, $table) = @_; my $sth = $dbh->prepare("SELECT * FROM $table LIMIT 1"); $sth->execute(); return $sth->fetchrow_array(); }
As VSarkiss already mentioned, it makes little sense to pass the statement handle ($sth) back from the subroutine.
Pass the result set instead. Of course, you need to make sure that your query won't return too many rows...

--perlplexer

Replies are listed 'Best First'.
(cLive ;-) Re: Copy an object
by cLive ;-) (Prior) on Apr 26, 2002 at 07:13 UTC
    Quick note:
    # connect my $dbh = DBI->connect_cached(...) # then add sub DESTROY { if (defined $dbh) { $dbh->disconnect(); } }
    This works for me and lets me forget about the disconnect.

    The connect_cached only reconnects if cached connection doesn't exist. So you can safely put a connect_cached before a query if you're unsure whether you're connected without the performance hit of connecting/disconnecting all the time. docs here

    Putting the disconnect in the DESTROY sub cleanly disconnects when the script exits - assuming, of course, that it will still be in scope :)

    AFAIK! No-one has told me otherwise, anyway :)

    .02

    cLive ;-)