简体   繁体   中英

perl dbi : fetchrow_arrayref

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

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.

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.

处理查询结果的另一种方法,您可以对其进行循环:

while(my $res = $sth->fetchrow_arrayref()) {do something}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM