commit 4f84b9bdc9a230240718288d8458969089751846
parent 074b919d89b10bbf0715d6c6e0b75ad0d6166b5c
Author: Kyle Milz <kyle@getaddrinfo.net>
Date: Sun, 12 Oct 2014 22:12:51 -0600
Shared: open the db in a common function
Diffstat:
3 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/Shared.pm b/Shared.pm
@@ -5,7 +5,7 @@ use Config::Grammar;
use Exporter;
@ISA = ("Exporter");
-@EXPORT = ("get_dom", "get_config");
+@EXPORT = ("get_dom", "get_config", "get_dbh");
sub get_dom
{
@@ -55,4 +55,16 @@ sub get_config
return $parser->parse($cfg_file) or die "ERROR: $parser->{err}\n";
}
+sub get_dbh
+{
+ my $cfg = shift;
+
+ my $dbh = DBI->connect(
+ "dbi:SQLite:dbname=$cfg->{general}{db_file}",
+ "",
+ "",
+ { RaiseError => 1 },) or die $DBI::errstr;
+ return $dbh;
+}
+
1;
diff --git a/price_scraper.pl b/price_scraper.pl
@@ -16,12 +16,7 @@ my %args;
getopts('f:np:v', \%args);
my $cfg = get_config($args{f});
-
-my $dbh = DBI->connect(
- "dbi:SQLite:dbname=$cfg->{general}{db_file}",
- "",
- "",
- { RaiseError => 1 },) or die $DBI::errstr;
+my $dbh = get_dbh($cfg);
$| = 1 if ($args{v});
diff --git a/product_scraper.pl b/product_scraper.pl
@@ -18,15 +18,10 @@ my %args;
getopts("vf:", \%args);
my $cfg = get_config($args{f});
+my $dbh = get_dbh($cfg);
$| = 1 if ($args{v});
-my $dbh = DBI->connect(
- "dbi:SQLite:dbname=$cfg->{general}{db_file}",
- "",
- "",
- { RaiseError => 1 },) or die $DBI::errstr;
-
$dbh->do("create table if not exists products(" .
"part_num text not null primary key, " .
"brand text, " .