pricecharts

track prices of consumer electronics
Log | Files | Refs | README

commit 48c38bc09923a8b187f34f4f62ff984f15b58685
parent 7b21b6895eacb6465127c62b30cb168813d46d73
Author: Kyle R W Milz <kyle@getaddrinfo.net>
Date:   Wed, 13 Aug 2014 01:08:46 -0600

price_scraper: implement database support for price saving

Diffstat:
Mprice_scraper.pl | 23+++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/price_scraper.pl b/price_scraper.pl @@ -113,7 +113,7 @@ while (my ($name, $vendor) = each ($cfg->{vendors})) { $price =~ s/,//; print substr($name, 0, 1); - $prices{$name} = $price; + $prices{"\"$name\""} = $price; } print '] (' . (time - $time_start) . " s)\n"; @@ -121,4 +121,23 @@ if ($args{v}) { print "$_: $prices{$_}\n" for (keys %prices); } -exit if ($args{n} || (scalar(keys %prices)) == 0); +if ($args{n} || (scalar(keys %prices)) == 0) { + $dbh->disconnect(); + exit; +} + +$dbh->do("create table if not exists [$part_no]" . + "(date int not null primary key)"); + +my $sth = $dbh->prepare("select * from [$part_no]"); +my @columns = @{$sth->{NAME}}; +for my $vendor (keys %prices) { + next if (grep {"\"$_\"" eq $vendor} @columns); + $dbh->do("alter table [$part_no] add column $vendor"); +} +$dbh->do("insert into [$part_no](date, " . + join(", ", keys %prices) . ") " . + "values ($time_start, " . + join(", ", values %prices) . ")"); + +$dbh->disconnect();