Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am writing an app that uses Finance::QuoteHist to fetch historical stock prices. My problem is that I want to read from a database a list of symbols that at last count was over 1500 entries long and I run out of memory trying to fetch all of this data at once. It appears that there is no way of setting the list of quotes other then using the new function which would like an array reference of stock symbols. What I would like to do is select the symbols list from the database and then fetch the data for each symbol one at a time instead of all at once, does any one have any suggestions?

Here is the code

#!/usr/bin/perl -w use strict; use DBI; use Finance::QuoteHist; my $user="me"; my $password="pass"; my $database="stocks"; my @data; my $i=0; my $dbh=DBI->connect("dbi:mysql:$database",$user,$password) or die "Ca +nnot connect to database"; $dbh->{raiserror} = 1; my $sth=$dbh->prepare("Select symbol from symbols;"); $sth->execute(); while(my @row=$sth->fetchrow_array()){ $data[$i]=$row[0]; $i++; } #Here is what I am having trouble with. my $q = new Finance::QuoteHist( symbols =>\@data, start_date => '01/01/01', end_date => 'today'); my @quotes = $q->quotes(); my $sth=$dbh->prepare("INSERT INTO stocks(symbol,date,open,high,low,cl +ose,volume,adjusted) VALUES(?,?,?,?,?,?,?,?);"); for my $i (0 .. $#quotes){ $sth->execute($quotes[$i][0],$quotes[$i][1],$quotes[$i][2],$quotes +[$i][3],$quotes[$i][4],$quotes[$i][5],$quotes[$i][6],$quotes[$i][7])| +| die "error: ", $dbh->errstr; } #if any one knows how to make this a little bit prettier I would also +appreciate it!! $dbh->disconnect();
Thanks in advance

Will

Edit: chipmunk 2001-07-16

Replies are listed 'Best First'.
Re: Help with Finance::QuoteHist
by Zaxo (Archbishop) on Jul 16, 2001 at 10:56 UTC

    This will allow you to grab less data at one whack. I'll also throw in some prettier constructs which use fewer temporary variables.

    #!/usr/bin/perl -w use strict; use DBI; use Finance::QuoteHist; my $user="me"; my $password="pass"; my $database="stocks"; my $dbh=DBI->connect("dbi:mysql:$database",$user,$password,{RaiseError +=>1}) or die "Cannot connect to database"; my $syms = $dbh->selectcol_arrayref("Select symbol from symbols"); my $sth=$dbh->prepare("INSERT INTO stocks (symbol,date,open,high,low,close,volume,adjuste +d) VALUES(?,?,?,?,?,?,?,?)"); for (@{$syms}) { my $q = new Finance::QuoteHist( symbols => [$_], # just one symbol start_date => '01/01/2001', end_date => 'today'); for ($q->quotes()){ $sth->execute(@{$_}); } #next stock symbol now } # done $dbh->disconnect();

    The selectcol_arrayref call is relatively new in DBI.pm. If it's unavailable, what you have will work just fine.

    After Compline,
    Zaxo