List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:August 24 1999 6:39pm
Subject:Re: update? insert? Perl script
View as plain text  
At 1:11 PM -0500 8/24/99, gl3 wrote:
>Hi,
>
>I'm having a strange problem with update in a Perl script I have. When I
>try the following:
>
>$dbh = $sth->prepare("select $id from $table");
>$sth->execute;
>$yes = $sth->fetchrow;
>if ($yes ne "") {
>	$dbh = $sth->prepare("update $table set q = "\$q\" where id =
>\"$id\"");
>	$sth->execute;
>}
>else {
>	$sth->prepare("insert into $table set id = \"$id\", set q =
>"\$q\"");
>	$sth->execute;
>}
>
>it inserts a new id of the same value even if $yes eq "". I think my
>problem is with setting $yes, though I'm not sure. Should I be using
>replace instead?


If you want to determine whether or not such a row exists, then the
value of fetchrow will be undef (not "") if there is no such row.
You may want something like this:

if (defined ($yes)) # row exists
{
	update
}
else                # row does not exist
{
	insert
}

-- 
Paul DuBois, paul@stripped
Thread
update? insert? Perl scriptgl324 Aug
  • Re: update? insert? Perl scriptJeff Beard24 Aug
  • Re: update? insert? Perl scriptPaul DuBois24 Aug
    • RE: update? insert? Perl scriptDan Ray24 Aug
      • Re: update? insert? Perl scriptThimble Smith24 Aug
      • RE: update? insert? Perl scriptgl324 Aug
      • Re: update? insert? Perl scriptScott Hess24 Aug
      • RE: update? insert? Perl scriptPaul DuBois24 Aug
        • RE: update? insert? Perl scriptJay Miller24 Aug
          • RE: update? insert? Perl scriptMichael Widenius29 Aug
        • RE: update? insert? Perl scriptDan Ray25 Aug
  • Re: update? insert? Perl scriptB.W. McAdams24 Aug