Re: Why is $sth->rows() uninitialized?

KEVIN ZEMBOWER <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Greg, thanks for your suggestion. Oddly enough, I made the change, but the output was exactly the same, both the trace() output and the output I wrote. Something really odd seems to be going on.

At the risk of a really long email message, I've pasted in the mysqldump of the database, without any data, the program and the datafile. I hope I trimmed out correctly the tables not used by this program.

Thanks, again.

-Kevin

>>> Greg Meckes <[email protected]> 03/29/05 04:12PM >>>
It's possible that the value of literal zero being returned is being interpreted as uninitialized
by Perl.

You should try something like this:

if (! defined($sth->rows()) || $sth->rows() == 0)) {
print STDERR "Duplicate item found at line $ln: $_\n";
}

================================================
kevinz@www:~/public_html/orderDB/scripts$ mysqldump --databases orderDB -a -d
-- MySQL dump 8.21
--
-- Host: localhost    Database: orderDB
---------------------------------------------------------
-- Server version       3.23.49-log

--
-- Current Database: orderDB
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ orderDB;

USE orderDB;

--
-- Table structure for table 'baseitem'
--

CREATE TABLE baseitem (
  baseitemid int(11) NOT NULL auto_increment,
  subcatid int(11) NOT NULL default '0',
  projectid int(11) NOT NULL default '0',
  partno varchar(25) NOT NULL default '',
  poplineid varchar(25) default NULL,
  PRIMARY KEY  (baseitemid),
  UNIQUE KEY partno (partno)
) TYPE=MyISAM COMMENT='Items in inventory (regardless of language)';

--
-- Table structure for table 'langversions'
--

CREATE TABLE langversions (
  langversionid int(11) NOT NULL auto_increment,
  baseitemid int(11) NOT NULL default '0',
  langid int(11) NOT NULL default '0',
  url varchar(255) default NULL,
  title varchar(255) NOT NULL default '',
  description text,
  available enum('Y','N') NOT NULL default 'Y',
  cost decimal(8,2) default NULL,
  PRIMARY KEY  (langversionid),
  KEY langid (langid),
  KEY baseitemid (baseitemid)
) TYPE=MyISAM COMMENT='Language versions of base items in inventory';

kevinz@www:~/public_html/orderDB/scripts$ 
======================================
kevinz@www:~/public_html/orderDB/scripts$ cat ./loadInventory.pl 
#!/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;
   if ($available == 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;
   
   my $sth = $dbh->prepare("SELECT baseitemid FROM langversions WHERE baseitemid =? AND langid=?");
   $sth->execute($baseitemid, $langid);
   
   if (! defined($sth->rows()) || $sth->rows() == 0) {
      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";
   } # 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
kevinz@www:~/public_html/orderDB/scripts$ 
==============================================
kevinz@www:~/public_html/orderDB/scripts$ cat ../tmp/a 
"B-B01","Eng","Binder for Complete Set of Population Reports",13,0
kevinz@www:~/public_html/orderDB/scripts$ 

-- 
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe:    http://lists.mysql.com/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.