Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i keep getting a message saying this dbi command is not correctly ended
my $sth3 = $dbh->do("UPDATE league SET played = \'$playeda\' AND SET w +on = \'$wona\' AND SET fr = \'$fora\' AND SET ag = \'$againsta\' AND +SET points = \'$pointsa\' WHERE year = \'$thisyear\' AND sport = \'$s +port\' AND group = \'$group\'")|| die "Can't execute SQl update 1";
any ideas

Replies are listed 'Best First'.
Re: perl dbi problem1
by knobunc (Pilgrim) on Apr 12, 2001 at 22:50 UTC

    Your problem is that the UPDATE syntax is wrong. You want to say:

    my $statement = qq( UPDATE LEAGUE SET played = ?, won = ?, fr = ?, ag = ?, points = ? WHERE year = ? AND sport = ? AND group = ? ); $dbh->do($statement, {}, $playeda, $wona, $fora, $againsta, $pointsa, $thisyear, $sport, $group) || die "SQL error in update 1";

    Besides fixing the syntax, I used bind variables in the do to allow DBI to escape the strings for me correctly to make sure that all SQL metacharacters are safe. The qq() thing just makes up a string (but I find it more readable to write my SQL queries that way).

    Hope that helps.

    -ben

Re: perl dbi problem1
by Masem (Monsignor) on Apr 12, 2001 at 22:40 UTC
    If any of those fields that you have in that statement have a quote (either ' or ") you're going to have problems.

    Instead, it's probably better to use placeholders for this case--DBI will automagically escape any quotes in the values that you pass to protect from this problem.

    my $sth3 = $dbh->prepare( q( UPDATE league SET played=?, won=?, fr=?, ag=?, points=? WHERE year=?, sport=?, group=? ) ) or die DBI->errstr; $sth3->execute( $playeda, $wona, $fora, $againsta, $pointsa, $thisyear, $sport, $group ) or die DBI->errstr;

    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: perl dbi problem1
by physi (Friar) on Apr 12, 2001 at 22:43 UTC
    Try:
    my $sth3 = $dbh->do("UPDATE league SET played = '$playeda', won = '$wo +na', fr = '$fota', ag = '$againsta', points = '$pointsa', year= '$thi +syear', sport= '$sport', group = '$group'")
    all the  AND's are not nesessary. And you don't need to add a \ before your '.

    Hope this will work & help.

    ----------------------------------- --the good, the bad and the physi-- -----------------------------------