Perl: Reading from a database table line by line
Is there someway to read from 开发者_如何学Pythona database table line by line using the DBI module? The table is huge and I keep running out of memory if I try to do a select. I'm working with an oracle database
The problems lies in your query. You must restrain your results set.
As mentioned by DavidO, LIMIT is a solution.
my $sth = $dbh->prepare("SELECT whatever FROM wherever LIMIT 50");
$sth->execute;
my $row;
while (my @data = $sth->fetchrow_array) {
print "my $row(@row)";
sleep(1);
}
$sth->finish;
$dbh->disconnect;
Many databases support a "LIMIT" clause in the SQL. Or you could SELECT given a range of primary keys with a WHERE clause to constrain how much data you get back per query.
Yes. If you don't want to get all the results at once, get them with a DBI function that doesn't have all in its name. e.g.,
my $sth = $dbh->prepare("SELECT whatever FROM wherever");
$sth->execute;
while (my @data = $sth->fetchrow_array) { # returns just one row
# do stuff with @data
}
精彩评论