in reply to Updating a database

Or...
$sql = <<EOF; update Volunteer set volunteer_id = $new_id and Number = $newnumbe +r where id = 2 EOF ; $dbh->do($sql) || return "ERROR: '$sql'";

...where $dbh is the database handle

Replies are listed 'Best First'.
Re: Re: Updating a database
by grep (Monsignor) on Jan 15, 2002 at 10:46 UTC
    The problem with this approach is you do not get the proper quoting that placeholders provide. You sql would break if

    $new_id = "I'm tired"

    grep
    grep> cd pub grep> more beer
Re: Re: Updating a database
by George_Sherston (Vicar) on Jan 15, 2002 at 14:41 UTC
    As grep. Also, if you don't use placeholders then there's no need to assign the query to a var before doing it. And the DBI error string would be interesting if it dies. If I wasn't going to use place holders I'd do
    $dbh->do("update Volunteer set volunteer_id = $new_id and Number = $ne +wnumber where id = 2") or die $dbh->errstr;
    ... except for grep's reason I *would* use placeholders, so I'd use a slight tweak on dws:
    my $sth = $dbh->prepare("UPDATE Volunteer " . "SET Volunteer = ? " . "WHERE Date = ? AND Volunteer = 'TBD' AND Number = ?" ) or die $dbh->errstr; my $rv = $sth->execute($status, $date, $id) or die $dbh->errstr;


    § George Sherston