Aks
Aks

Reputation: 5236

Perl: Reading from a database table line by line

Is there someway to read from a 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

Upvotes: 1

Views: 1130

Answers (3)

Rgonomike
Rgonomike

Reputation: 338

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;

Upvotes: 2

Dave Sherohman
Dave Sherohman

Reputation: 46245

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
}

Upvotes: 1

DavidO
DavidO

Reputation: 13942

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.

Upvotes: 1

Related Questions