Checking for existence in records?
KEVIN ZEMBOWER <[email protected]>
| Newsgroups | gmane.comp.db.mysql.perl |
|---|---|
| Message-ID | <[email protected]> |
I'm trying to check if an input record exists in a table, and if it doesn't, add it, or if it does, go on to the next input. The code I wrote is:
#my $duplangveritem = $dbh->selectrow_array("SELECT baseitemid FROM langversions WHERE baseitemid ='$baseitemid' AND langid='$langid'");
my $sth = $dbh->prepare("SELECT baseitemid FROM langversions WHERE baseitemid ='$baseitemid' AND langid='$langid'");
$sth->execute();
my $duplangveritem = $sth->fetchrow_array;
if ($duplangveritem) {
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
I tried both the first, commented-out line, or the next three lines, but the line with 'if ($duplangveritem) {' still causes errors like these (the 'if ($duplangveritem)' line is line 72):
Use of uninitialized value in concatenation (.) or string at ./loadInventory.pl line 72. #NOTE: This is unwanted error
Duplicate item found at line 2: #NOTE: This is intended output
Use of uninitialized value in concatenation (.) or string at ./loadInventory.pl line 72.
Duplicate item found at line 15:
How can I check if the item already exists in the database, and not get an error if it doesn't?
Thanks for all your help.
-Kevin Zembower
--
MySQL Perl Mailing List
For list archives: http://lists.mysql.com/perl
To unsubscribe: http://lists.mysql.com/[email protected]