Why is $sth->rows() uninitialized?

Why is $sth->rows() uninitialized?

am 29.03.2005 21:43:06 von KEVIN ZEMBOWER

Thanks to the suggestions of Greg and others, I was able to use $sth-rows()=
to determine if a record was present or not in my table. Thanks, Greg. =
The section of code I used successfully is:
$kwsth->execute($_) or warn "Problem with execute: $DBI::errstr\n";
=20
if ($kwsth->rows() == 0) {=20
print "Keyword $_ not found " if $debug;=20
$inskwsth->execute($_);
$keywordid =3D $dbh->{'mysql_insertid'};
print "NEW id =3D $keywordid\n" if $debug;
=20
} else { # If the keyword doesn't already exist in the table, =
else...
=20
$keywordid =3D $kwsth->fetchrow_array or warn "Problem with =
fetch: $DBI::errstr\n";
print "Keyword ID: $keywordid\n" if $debug;
} # else if the keyword DOES exist in the table already


Now I'm trying to apply this to another program in an almost identical =
fashion, and don't understand why I'm getting an error. The section of =
code is:
my $sth =3D $dbh->prepare("SELECT baseitemid FROM langversions WHERE =
baseitemid =3D? AND langid=3D?");
$sth->execute($baseitemid, $langid);
=20
if ($sth->rows() == 0) { #THIS is line 70
print STDERR "Duplicate item found at line $ln: $_\n";
} else {
my $sth =3D $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

When I execute this with trace(2) set, I get this section of the output:
<- prepare=3D DBI::st=3DHASH(0x828b1f0) at loadInventory.pl line 67
-> execute for DBD::mysql::st (DBI::st=3DHASH(0x828b1f0)~0x81e27e0 '1' =
'1')
-> dbd_st_execute for 0828b220
Binding parameters: SELECT baseitemid FROM langversions WHERE =
baseitemid =3D'1' AND langid=3D'1'
<- dbd_st_execute 0 rows
<- execute=3D '0E0' at loadInventory.pl line 68
-> rows for DBD::mysql::st (DBI::st=3DHASH(0x828b1f0)~0x81e27e0)
<- rows=3D '0' at loadInventory.pl line 70
Use of uninitialized value in concatenation (.) or string at ./loadInventor=
y.pl line 70.
Duplicate item found at line 1:=20

The last line is my STDERR output. The previous line is an error message. =
The trace() output seems to show rows() being set to '0' at line 70, =
which is "if ($sth->rows() == 0) {".

I don't understand perl says $sth->rows() isn't initialized, when it seems =
to be set to zero, and why this system isn't working in this program, but =
seems to work fine in my other one.

Thanks for any light you can shed on this problem.

-Kevin

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=3Dgcdmp-msql-mysql-modules @m.gmane.org

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

am 29.03.2005 23:12:30 von Greg Meckes

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";
}


--- KEVIN ZEMBOWER wrote:
> Thanks to the suggestions of Greg and others, I was able to use $sth-rows() to determine if a
> record was present or not in my table. Thanks, Greg. The section of code I used successfully is:
> $kwsth->execute($_) or warn "Problem with execute: $DBI::errstr\n";
>
> if ($kwsth->rows() == 0) {
> print "Keyword $_ not found " if $debug;
> $inskwsth->execute($_);
> $keywordid = $dbh->{'mysql_insertid'};
> print "NEW id = $keywordid\n" if $debug;
>
> } else { # If the keyword doesn't already exist in the table, else...
>
> $keywordid = $kwsth->fetchrow_array or warn "Problem with fetch: $DBI::errstr\n";
> print "Keyword ID: $keywordid\n" if $debug;
> } # else if the keyword DOES exist in the table already
>
>
> Now I'm trying to apply this to another program in an almost identical fashion, and don't
> understand why I'm getting an error. The section of code is:
> my $sth = $dbh->prepare("SELECT baseitemid FROM langversions WHERE baseitemid =? AND
> langid=?");
> $sth->execute($baseitemid, $langid);
>
> if ($sth->rows() == 0) { #THIS is line 70
> 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
>
> When I execute this with trace(2) set, I get this section of the output:
> <- prepare= DBI::st=HASH(0x828b1f0) at loadInventory.pl line 67
> -> execute for DBD::mysql::st (DBI::st=HASH(0x828b1f0)~0x81e27e0 '1' '1')
> -> dbd_st_execute for 0828b220
> Binding parameters: SELECT baseitemid FROM langversions WHERE baseitemid ='1' AND
> langid='1'
> <- dbd_st_execute 0 rows
> <- execute= '0E0' at loadInventory.pl line 68
> -> rows for DBD::mysql::st (DBI::st=HASH(0x828b1f0)~0x81e27e0)
> <- rows= '0' at loadInventory.pl line 70
> Use of uninitialized value in concatenation (.) or string at ./loadInventory.pl line 70.
> Duplicate item found at line 1:
>
> The last line is my STDERR output. The previous line is an error message. The trace() output
> seems to show rows() being set to '0' at line 70, which is "if ($sth->rows() == 0) {".
>
> I don't understand perl says $sth->rows() isn't initialized, when it seems to be set to zero,
> and why this system isn't working in this program, but seems to work fine in my other one.
>
> Thanks for any light you can shed on this problem.
>
> -Kevin
>
> --
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe: http://lists.mysql.com/perl?unsub=gregmeckes@yahoo.com
>
>



__________________________________
Do you Yahoo!?
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

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

am 29.03.2005 23:34:09 von KEVIN ZEMBOWER

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 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";
}

==================== =====3D=
==================== ===3D
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=3DMyISAM COMMENT=3D'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=3DMyISAM COMMENT=3D'Language versions of base items in inventory';

kevinz@www:~/public_html/orderDB/scripts$=20
==================== =====3D=
=============3D
kevinz@www:~/public_html/orderDB/scripts$ cat ./loadInventory.pl=20
#!/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 =3D 1; #Set to true to output debugging information

my $dbh =3D DBI->connect("DBI:mysql:database=3DorderDB;host=3Dlocalhost" , =
"orderDBadmin", "password",
{ RaiseError =3D> 1 } #Use die() with errors
);

DBI->trace(2);

$dbh->do("delete from baseitem");
$dbh->do("delete from langversions");

my $ln =3D 0; #Line number for output

my $parser =3D Text::CSV::Simple->new({'binary' =3D> 1}); #Binary allows =
extra-ASCII char in Title

#my @data =3D $parser->read_file("../tmp/DistInventory.txt");
my @data =3D $parser->read_file("../tmp/a");
for my $aref (@data) {
$ln++;
my ($partno, $language, $title, $cost, $available) =3D @$aref;
print "PN=3D$partno, L=3D$language, T=3D$title, C=3D$cost, A=3D$availabl=
e\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) =3D @_;
print "TITLE is now $title\n" if $debug;
=20
print "Available:$available:\n" if $debug;
if ($available == 1) { $available =3D 'Y' } else { $available =3D =
'N' }; #Change truth code to Y or N
print "Available:$available:\n" if $debug;

my $langid; #Store the language id code here.=20
CASE: {
if ($language eq "Eng") { $langid =3D "1"; last CASE; }
if ($language eq "Fre") { $langid =3D "2"; last CASE; }
if ($language eq "SPA") { $langid =3D "3"; last CASE; }
if ($language eq "POR") { $langid =3D "4"; last CASE; }
if ($language eq "ARA") { $langid =3D "5"; last CASE; }
if ($language eq "SWA") { $langid =3D "6"; last CASE; }
if ($language eq "RUS") { $langid =3D "7"; last CASE; }
if ($language eq "TUR") { $langid =3D "8"; last CASE; }
if ($language eq "NTS") { $partno .=3D "-" . $language; $langid =3D =
"1"; last CASE; }
if ($language eq "PAL") { $partno .=3D "-" . $language; $langid =3D =
"1"; last CASE; }
if ($language eq "SEC") { $partno .=3D "-" . $language; $langid =3D =
"1"; last CASE; }
print STDERR "UNKNOWN Language in record $ln\n";
} #CASE statement on language
=20
my $baseitemid =3D createbaseitem($partno);
print "Base item ID =3D $baseitemid\n" if $debug;
=20
my $sth =3D $dbh->prepare("SELECT baseitemid FROM langversions WHERE =
baseitemid =3D? AND langid=3D?");
$sth->execute($baseitemid, $langid);
=20
if (! defined($sth->rows()) || $sth->rows() == 0) {
print STDERR "Duplicate item found at line $ln: $_\n";
} else {
my $sth =3D $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) =3D shift;
my $sth;

my $q =3D qq{=20
SELECT baseitemid=20
FROM baseitem=20
WHERE partno=3D'$partno'
};
print $q if ($debug == 2);

my $baseitemid =3D $dbh->selectrow_array(qq{
SELECT baseitemid=20
FROM baseitem=20
WHERE partno=3D'$partno'
});
=20
if (! $baseitemid) { #base item was NOT found in table
my $sth =3D $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 =3D $dbh->{'mysql_insertid'};
print "Inserted NEW item with id $baseitemid\n" if $debug;
=20
} else { #if base item was NOT found in table, otherwise ...
=20
print "Found EXISTING item with id $baseitemid\n" if $debug;

} # else if base item WAS found in table
=20
return $baseitemid;
} #sub createbaseitem
kevinz@www:~/public_html/orderDB/scripts$=20
==================== =====3D=
==================== =3D
kevinz@www:~/public_html/orderDB/scripts$ cat ../tmp/a=20
"B-B01","Eng","Binder for Complete Set of Population Reports",13,0
kevinz@www:~/public_html/orderDB/scripts$=20

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=3Dgcdmp-msql-mysql-modules @m.gmane.org

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

am 29.03.2005 23:39:24 von Rudy Lippan

On Tue, 29 Mar 2005, KEVIN ZEMBOWER wrote:

pandora:~:$ perl -lwe '1 if 0==undef'
Use of uninitialized value in numeric eq (==) at -e line 1.


> if ($sth->rows() == 0) { #THIS is line 70
> print STDERR "Duplicate item found at line $ln: $_\n";
> } else {

> Use of uninitialized value in concatenation (.) or string at ./loadInventory.pl line 70.
> Duplicate item found at line 1:
>
> The last line is my STDERR output. The previous line is an error message. The
> trace() output seems to show rows() being set to '0' at line 70, which is "if
> ($sth->rows() == 0) {".
>
> I don't understand perl says $sth->rows() isn't initialized, when it seems to
> be set to zero, and why this system isn't working in this program, but seems
> to work fine in my other one.
>
> Thanks for any light you can shed on this problem.
>

My guess is that $_ is undef.



-r


--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

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

am 30.03.2005 01:23:17 von Greg Meckes

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/perl?unsub=gcdmp-msql-mysql-modules@m .gmane.org

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

am 01.04.2005 22:05:23 von KEVIN ZEMBOWER

Rudy, Garry, Greg, thank you all so much for your help. Rudy, you get the =
prize: it was $_ which was uninitialized. I played with a few fixes, then =
just eliminated it in the output of the print STDERR command. Don't know =
why I couldn't fix or detect it, but since this was just supposed to be =
quick'n'dirty script, and I'd already blown the 'quick' part, I just cut =
it out. Also don't know why the error pointed to the previous line.

I'm continually amazed at the helpfulness of the Open Source community. =
I've never gotten as thoughtful and complete and quick responses to my =
questions from commercial software vendors. Thanks, again, so much.

-Kevin

>>> Rudy Lippan 03/29/05 04:39PM >>>
On Tue, 29 Mar 2005, KEVIN ZEMBOWER wrote:

pandora:~:$ perl -lwe '1 if 0==undef'
Use of uninitialized value in numeric eq (==) at -e line 1.


> if ($sth->rows() == 0) { #THIS is line 70
> print STDERR "Duplicate item found at line $ln: $_\n";
> } else {

> Use of uninitialized value in concatenation (.) or string at ./loadInvent=
ory.pl line 70.
> Duplicate item found at line 1:=20
>=20
> The last line is my STDERR output. The previous line is an error =
message. The
> trace() output seems to show rows() being set to '0' at line 70, which =
is "if
> ($sth->rows() == 0) {".
>=20
> I don't understand perl says $sth->rows() isn't initialized, when it =
seems to
> be set to zero, and why this system isn't working in this program, but =
seems
> to work fine in my other one.
>=20
> Thanks for any light you can shed on this problem.
>=20

My guess is that $_ is undef.



-r


--=20
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl=20
To unsubscribe: http://lists.mysql.com/perl?unsub=3Dkzembowe@jhuccp.org

--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/perl?unsub=3Dgcdmp-msql-mysql-modules @m.gmane.org