Re: Why is $sth->rows() uninitialized?
Greg Meckes <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
Kevin, I made a few minor changes to try. You should see them:
#!/usr/bin/perl -w
# loadInventory.pl loads the distribution inventory data, based on an export file from IPOD
use strict;
use warnings;
#use diagnostics; #NOTE: can't involk with Text::CSV::Simple
use DBI;
use Text::CSV::Simple;
my $debug = 1; #Set to true to output debugging information
my $dbh = DBI->connect("DBI:mysql:database=orderDB;host=localhost", "orderDBadmin", "password",
{ RaiseError => 1 } #Use die() with errors
);
DBI->trace(2);
$dbh->do("delete from baseitem");
$dbh->do("delete from langversions");
my $ln = 0; #Line number for output
my $parser = Text::CSV::Simple->new({'binary' => 1}); #Binary allows extra-ASCII char in Title
#my @data = $parser->read_file("../tmp/DistInventory.txt");
my @data = $parser->read_file("../tmp/a");
for my $aref (@data) {
$ln++;
my ($partno, $language, $title, $cost, $available) = @$aref;
print "PN=$partno, L=$language, T=$title, C=$cost, A=$available\n"
if $debug;
createlangversion($partno, $language, $title, $cost, $available);
} #while there are more lines in the import data file
$dbh->disconnect();
exit; #End of main routine; only subroutines below
sub createlangversion {
my ($partno, $language, $title, $cost, $available) = @_;
print "TITLE is now $title\n" if $debug;
print "Available:$available:\n" if $debug;
#The statement below will likely return "N" all the time
#because the value of $available is always going to be a string coming
#in like it is. Might be better to use: if ($available eq '1')
#if ($available == 1) { $available = 'Y' } else { $available = 'N' };
#################### made change here
if ($available eq '1') { $available = 'Y' } else { $available = 'N' };
#Change truth code to Y or N
print "Available:$available:\n" if $debug;
my $langid; #Store the language id code here.
CASE: {
if ($language eq "Eng") { $langid = "1"; last CASE; }
if ($language eq "Fre") { $langid = "2"; last CASE; }
if ($language eq "SPA") { $langid = "3"; last CASE; }
if ($language eq "POR") { $langid = "4"; last CASE; }
if ($language eq "ARA") { $langid = "5"; last CASE; }
if ($language eq "SWA") { $langid = "6"; last CASE; }
if ($language eq "RUS") { $langid = "7"; last CASE; }
if ($language eq "TUR") { $langid = "8"; last CASE; }
if ($language eq "NTS") { $partno .= "-" . $language; $langid = "1"; last CASE; }
if ($language eq "PAL") { $partno .= "-" . $language; $langid = "1"; last CASE; }
if ($language eq "SEC") { $partno .= "-" . $language; $langid = "1"; last CASE; }
print STDERR "UNKNOWN Language in record $ln\n";
} #CASE statement on language
my $baseitemid = createbaseitem($partno);
print "Base item ID = $baseitemid\n" if $debug;
#################### made change here
my $sth = $dbh->prepare("SELECT baseitemid FROM langversions WHERE baseitemid ='$baseitemid'
AND langid='$langid'");
$sth->execute;
my $baseitemidFromDb = "";
while (my @data = $sth->fetchrow_array()) {
$baseitemidFromDb = $data[0];
}
if ($baseitemidFromDb eq "") { #Nothing found
print STDERR "Duplicate item found at line $ln: $_\n";
} else {
my $sth = $dbh->prepare("INSERT INTO langversions (baseitemid, langid, title, cost,
available) VALUES (?, ?, ?, ?, ?)" ) or die "Can't prepare statement: $DBI::errstr";
$sth->execute($baseitemid, $langid, $title, $cost, $available)or die "Can't execute
statement: $DBI::errstr";
####################To Here
} # else if there was no duplicate item
} #sub createlangversion(partno, language, title, cost, available)
sub createbaseitem {
my ($partno) = shift;
my $sth;
my $q = qq{
SELECT baseitemid
FROM baseitem
WHERE partno='$partno'
};
print $q if ($debug == 2);
my $baseitemid = $dbh->selectrow_array(qq{
SELECT baseitemid
FROM baseitem
WHERE partno='$partno'
});
if (! $baseitemid) { #base item was NOT found in table
my $sth = $dbh->prepare("INSERT INTO baseitem (partno) VALUES
(?)")
or die "Can't prepare statement: $DBI::errstr";
$sth->execute($partno)
or die "Can't execute statement: $DBI::errstr";
$baseitemid = $dbh->{'mysql_insertid'};
print "Inserted NEW item with id $baseitemid\n" if $debug;
} else { #if base item was NOT found in table, otherwise ...
print "Found EXISTING item with id $baseitemid\n" if $debug;
} # else if base item WAS found in table
return $baseitemid;
} #sub createbaseitem
__________________________________
Do you Yahoo!?
Yahoo! Mail - 250MB free storage. Do more. Manage less.
http://info.mail.yahoo.com/mail_250
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]