On Fri, Apr 26, 2002 at 02:02:15PM -0400, Shao, Chunning wrote:
>
> I need to plot a result set from DBI. Before I can do that, I need to
> know how many rows are returned, so I can set up my x, y axis right.
>
> I am doing
> #######################################################
> $csr = $dbh->prepare($sql);
> $csr->execute();
>
> #get the number that how many rows returned
> my $number_rows = 0;
> while (my @rows = $csr->fetchrow_array )
> {
> $number_rows +=1;
> }
> Set my x,y axis accordingly............
>
> #begin to plot the data
> my ($x_definition,$y_definition);
> while (my @row = $csr->fetchrow_array )
> {
> $counter = 0;
> foreach my $data (@row)
> Plot the data
> }
> ########################################################
> but I got an error
> 281
> DBD::Oracle::st fetchrow_array failed: ERROR no statement executing (perhaps you
>need to call execute first) at ./test.pl line 162.
>
> Do I have to make a second db call, or there is better way of doing it?
Once you've fetched all the rows from a statement handle, you can't go back
and fetch them again without re-executing the statement. Try something
like this:
$csr = $dbh->prepare($sql);
$csr->execute();
my $rows = $csr->fetchall_arrayref();
my $number_rows = @$rows;
# set up the plot
for my $row (@$rows) {
$counter = 0;
foreach my $data (@$row) {
# plot the data
}
}
Ronald