Utility Script: load_stocklist.pl

This script was used to load up the stock list with dummy data for testing.

Stock List

Flat file format, very simple:
64-2059954	12
64-137877	100
64-1201565	4
64-135517	21
64-2743953	2
64-17519887	3
64-3748919	5
  

load_stocklist.pl Script

#!/usr/local/bin/perl
use DBI;
use constant DB=>'dbi:mysql:perl_conference';

my $db = DBI->connect( DB,undef,undef,{PrintError=>0} )
    || die "Connect failure: ",$DBI::errstr;

my $insert = $db->prepare(<<END) || die $db->errstr;
INSERT INTO StockList (quantity,catalog)
   VALUES (?,?)
END

my $update = $db->prepare(<<END) || die $db->errstr;
UPDATE StockList SET quantity=? WHERE catalog=?
END
    ;

while (<>) {
    chomp;
    next unless my ($catalog,$quant) = split /\s+/;
    $insert->execute($quant,$catalog) || $update->execute($quant,$catalog)
	|| warn "Can't load $catalog: ",$db->errstr,"\n";
}

$db->disconnect;

__END__
Table Definition:

CREATE TABLE StockList
    (
     catalog   char(10) primary key,
     quantity  int default 0 not null
     );
  

<< Previous
Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Sun Apr 25 14:09:22 EDT 1999