on Thu, 13 Jun 2002 07:16:29 GMT, [EMAIL PROTECTED] (Philip M .
Gollucci) wrote: 

> I need to write a wrapper function for calls to
> $sth->fetchrow_array() where the SELECT statement will return
> multiple rows.
> 
> such that
> 
> sub do_sql_array ($@) {
>      # on initial call: $sth = $dbh->prepare(); $sth->execute();
>      # on initial and subsequent until no more rows: return on row
>      from #           $sth->fetchrow_array()
>      # on no more rows to return: return () - an emply list
> }

Something like this?

    #! /usr/bin/perl -w
    use strict;
    use DBI;
    
    my $dbh = DBI->connect('dsn', 'user', 'password'); 

    sub generate_wrapper {
        my $dbh = shift;
        my $sql = shift;        
        my @rest = @_;
        my $sth;
        return sub  { unless ($sth) { 
                         $sth = $dbh->prepare($sql); 
                         $sth->execute(@rest);
                      }
                                 $sth->fetchrow_array(); 
                    }
    }

    my $do_sql_array = generate_wrapper($dbh, 
                         "SELECT a, b FROM atable WHERE a = ?", 10); 

    while  (my @res = $do_sql_array->()) {
        print "@res\n";
    }
    $dbh->disconnect();

-- 
felix

Reply via email to