Re: Checking for entry in table?

Greg Meckes <[email protected]>
Newsgroups gmane.comp.db.mysql.perl
Message-ID <[email protected]>
Something like the following should work:

while (<>) {
$ln++;

my ($popno, $kws) = split("\t");
chomp($kws);

print "$popno:\n" if $debug;

my (@keywords) = split('\|', $kws);

    foreach my $kw (@keywords) {
    my $KEYWORD = uc($kw);
    print "\t$kw " if $debug;
    
    my $kwsth = $dbh->prepare("SELECT keywordid FROM keywords WHERE TRIM(keyword)='$KEYWORD'");
    $kwsth->execute;
    
    my $Found = "";
    while (my @data = $sth_GL->fetchrow_array()) {
    $Found = $data[0];
    }
    
        if ($Found ne "") {
        print "Keyword ID: $Found\n" if $debug;
        }
            else {
            print "Record not found;\n";
            }
    
    
    }


}


_____________________________________
--- KEVIN ZEMBOWER <[email protected]> wrote:
> I'm still trying to check whether a record exist in a table, and act accordingly. While I
> appreciate Garry's suggestion, I want to understand it fully, and I'm stuck.
> 
> My program has this loop in it:
> my $kwsth = $dbh->prepare("SELECT keywordid FROM keywords WHERE TRIM(keyword)=?");
> while (<>) { #While there's more lines in the file called on the command line, of POPLINE
> document numbers and keywords
>    $ln++;
>    my ($popno, $kws) = split("\t"); #split on the tab following the POPLINE number
>    chomp($kws);
>    print "$popno:\n" if $debug;
>    my (@keywords) = split('\|', $kws); #split on the pipe symbol that separates keywords
>    foreach (@keywords) {
>       $_ = uc($_);
>       print "\t$_ " if $debug;
>       my $kwsthrv = $kwsth->execute($_) or warn "Problem with execute: $DBI::errstr\n";
>       if (! $kwsthrv) { print "Keyword $_ not found\n" } else {
>          my $keywordid = $kwsth->fetchrow_array or warn "Problem with fetch: $DBI::errstr\n";
>          print "Keyword ID: $keywordid\n" if $debug;
>       }
>    } #foreach keyword in the array of keywords
> } #while there are more lines of input on the command line
> 
> The datafile looks like this:
> kevinz@www:~/public_html/orderDB/scripts$ cat ../tmp/w 
> 051877  Primary Health Care|Family Planning|Dummy
> kevinz@www:~/public_html/orderDB/scripts$ 
> 
> The output looks like this:
> kevinz@www:~/public_html/orderDB/scripts$ ./loadPOPkeywords.pl ../tmp/w
> 051877:
>         PRIMARY HEALTH CARE Keyword ID: 1791
>         FAMILY PLANNING Keyword ID: 743
> Use of uninitialized value in concatenation (.) or string at ./loadPOPkeywords.pl line 35, <>
> line 1.
> Problem with fetch: 
> Use of uninitialized value in concatenation (.) or string at ./loadPOPkeywords.pl line 36, <>
> line 1.
>         DUMMY Keyword ID: 
> kevinz@www:~/public_html/orderDB/scripts$
> 
> I want to detect, at either the execute or fetch phase, whether or not the record exists, and
> then either add it and get the mysql_insertid, or just fetch the id of the already inserted
> record. I don't want the errors that show up, or the warn() execution.
> 
> Thanks for your help and suggestions.
> 
> -Kevin Zembower
> 
> 
> --
> MySQL Perl Mailing List
> For list archives: http://lists.mysql.com/perl
> To unsubscribe:    http://lists.mysql.com/[email protected]
> 
> 


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Sports - Sign up for Fantasy Baseball. 
http://baseball.fantasysports.yahoo.com/

-- 
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.