FAQ
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
}

I know this can be down with a closure or an OBJECT, but for some reason
its escaping
me. Anything like this been done before ?

Anyone point me in the right direction ?

Search Discussions

  • Felix Geerinckx at Jun 13, 2002 at 8:20 am
    on Thu, 13 Jun 2002 07:16:29 GMT, philip@p6m7g8.com (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
  • Tim Bunce at Jun 13, 2002 at 9:09 am

    On Thu, Jun 13, 2002 at 03:16:29AM -0400, 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
    }
    Out of interest, why do you want/need it to work that way?

    Tim.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupdbi-users @
categoriesperl
postedJun 13, '02 at 7:18a
activeJun 13, '02 at 9:09a
posts3
users3
websitedbi.perl.org

People

Translate

site design / logo © 2022 Grokbase