FAQ
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

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
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