in reply to DBI Row Limiting

Just fetch the rows one at a time, up to your limit, then use $sth->finish to tell DBI you're done. On the databases for which it works, it does the job. On databases for which it doesn't (the whole query comes back regardless), you haven't lost anything.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re^2: DBI Row Limiting
by ketema (Scribe) on Sep 14, 2004 at 16:47 UTC
    I may do that if I have too, but I like to keep the structure of the program intact if possible. I am debugging it in a visual debugger, and I want to see the results of the call, just not ALL the results. I am assuming then that the answer is no to a DBI specific method that limits the number of rows returned?

      There in fact are method(s) that limit the number of results you get. They're called fetchrow_*(). They limit the number of results to one row so you can control however many you actually obtain. Straight out of the DBI documentation:

      $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth->execute( $baz ); while ( @row = $sth->fetchrow_array ) { print "@row\n"; }

      Now you can transform this a little to suit your needs like merlyn was talking about:

      my $LIMIT = 30; my $sth = $dbh->prepare("SELECT foo, bar FROM table WHERE baz=?"); $sth->execute( $baz ); my ( @rows, @row ); for ( 1 .. $LIMIT ) { last unless ( @row = $sth->fetchrow_array ); push @rows, [ @row ]; } #do stuff with @rows

      This code is untested but you get the general idea. There are other types of fetchrow_*() methods that you can take a look at. Read the DBI documentation.

      Update: You can also take a look at the fetchall_arrayref() method which appears to take a $max_rows parameter.

      Zenon Zabinski | zdog | zdog@perlmonk.org