in reply to Re^3: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at (Updates:2)
in thread DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at

Depending on the number of rows, fetching them all can by quite memory consuming. My code (although for DBD::Pg) usually looks like this:

use DBI; use Carp; use English; ... my $dbh = DBI->connect($self->{dburl}, $self->{dbuser}, $self->{dbpass +word}, { AutoCommit => 0, # This is a proper + database, not a key-value store RaiseError => 0, # Child protection + mode off. We are doing our own error checking, thank you very much AutoInactiveDestroy => 1, # Just do + the right thing when using fork() }) or croak("$EVAL_ERROR"); ... # sub bla($dbh) { my $selsth = $dbh->prepare_cached("SELECT foo, bar, baz FROM bli WHERE + bla = ? ORDER BY blub") or croak($dbh->errstr); # If the statement is wrong, no point +of internal error handling if(!$selsth->execute("foobar")) { $reph->debuglog($dbh->errstr); $dbh->rollback; return 0; # Tell caller we failed } # Work on the data row-by-row while((my $line = $selsth->fetchrow_array)) { ... # Do something with the data ... } $selsth->finish; # Make sure we don't leave database pointers and stuf +f around $dbh->commit; # Finish the current transaction, unlock all locked tabl +es/rows and stuff. # commit() may or may not happen in THIS function, depen +ding on if the caller needs to # chain multiple functions in a single transaction return 1; # tell caller we succeeded. ...

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics
  • Comment on Re^4: DBD::mysql::st fetchrow_hashref failed: fetch() without execute() at (Updates:2)
  • Download Code