tags:

views:

37

answers:

3

Hi all ,

Actually I have executed the postgres query, assume that it has return the 10 rows. Now I am having that's statement handler ( $sth ) .

print Dumper $sth->fetchrow_arrayref;
print Dumper $sth->fetchrow_arrayref;
print Dumper $sth->fetchrow_arrayref;
print Dumper $sth->fetchrow_arrayref;
print Dumper $sth->fetchrow_arrayref;

Now , I have fetched the 5 rows from the statement handler ( $sth .Now I want to get back $sth reference pointer to 1th row.....

What should I do....?

Thanks

+4  A: 

You cannot do that. Database cursors are designed to be read row by row, like a stream, without rewinding.

You need to copy the data into an array in memory if you want to jump around.

The easiest way would be to do

my $all_rows = $sth->fetchall_arrayref;

which gives you an arrayref, with one entry for each row, each row in the same format as fetchrow_arrayref produced.

Thilo
A: 

another way to process your query's result's, you can loop over it :

while(my $res = $sth->fetchrow_arrayref()) {do something}
benzebuth
+1  A: 

In a while loop.

  1. Fetch row.
  2. If it is something you want to keep, copy the results to a save list.
  3. Refer to the save list whenever you want to reference a previous row.
Sinan Ünür