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

Ive looked aroound everywhere, and I still dont see whats wron here
$dbh->do("UPDATE settings SET limit='$p'") or die $dbh->errstr;
??? I get this error
You have an error in your SQL syntax near 'limit='33'' at line 1 at ca +rt.pm line 1246.

Replies are listed 'Best First'.
Possible solutions
by BorgCopyeditor (Friar) on Jul 23, 2002 at 20:40 UTC

    Maybe because the value to be updated doesn't take quotation marks? Also, I know that limit != LIMIT, but maybe your column should be named something besides a (probable) reserved word?

    By the way, is there a better place to post this, such as an SQLMonks group? ;-) But seriously, that error string looks like it's produced by your database, not by perl.

    BCE
    --Your punctuation skills are insufficient!

Re: SQL do update is killing me
by dws (Chancellor) on Jul 23, 2002 at 20:47 UTC
    You have an error in your SQL syntax near 'limit='33'' at line 1 ...

    If you're using MySQL, "limit" is a reserved word, though you should have seen this earlier when you created the table.

Re: SQL do update is killing me
by gryphon (Abbot) on Jul 23, 2002 at 21:15 UTC

    Greetings "anonymous" (are you andrew?),

    Always use bind values or placeholders when possible. They'll take care of all the quoting issues that come up from time to time.

    my $p = 33; $dbh->do(q{ UPDATE settings SET limit = ? }, undef, $p) or die $dbh->errstr;

    The undef is important. It's where you would put a reference to an array detailing any attributes you want included in the do action. I've never used it yet, but you have to add the undef else things don't work.

    Spend some time scanning (or better yet reading) through the DBI documentation. You'll be glad you did.

    Update: Although I think the problem you're encountering is caused by a quoting issue, there's also the chance that "limit" is a reserved word for your particular database's SQL. Try renaming the column to something else and retrying the UPDATE query. (Actually, try a SELECT first.)

    -gryphon
    code('Perl') || die;

Re: SQL do update is killing me
by bilfurd (Hermit) on Jul 23, 2002 at 23:56 UTC
    A quick workaround:

    "UPDATE settings SET [limit]='$p'"OR
    "UPDATE settings SET [limit]=$p"

    Specifying the field name within the squares should take care of any reserved word conflicts. Double check the 'limit' field data type to make sure nobody changed it on you - there are times when our coworkers get creative without telling anyone...

Re: SQL do update is killing me
by simeon2000 (Monk) on Jul 23, 2002 at 20:52 UTC
    You may want to also check into prepare and execute in dbh, or the quoting function to avoid having extraneous unescaped quote characters in your queries.

    "Falling in love with map, one block at a time." - simeon2000