Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Re: Using placeholders in MySQL returning an error

by mpeppler (Vicar)
on Jan 16, 2004 at 17:01 UTC ( [id://321833]=note: print w/replies, xml ) Need Help??


in reply to Re: Using placeholders in MySQL returning an error
in thread Using placeholders in MySQL returning an error

I would avoid the  insert ... set foo = ?, ... syntax, as it is completely non-standard SQL.

Michael

Replies are listed 'Best First'.
Re: Re: Re: Using placeholders in MySQL returning an error
by bradcathey (Prior) on Jan 16, 2004 at 18:30 UTC
    Michael, thanks, but I would love to hear more. Code samples? Thanks.

    —Brad
    "A little yeast leavens the whole dough."
      Well - injunjoel mentioned the possibility of using the INSERT into foo SET bar = ?, ... syntax to execute an insert with placeholders. I just wanted to point out that using INSERT ... SET ... is not part of the SQL standard (AFAIK). It may be tempting to use because it is of course very similar to UPDATE ... SET ... WHERE, but your code will not be portable to other database engines if you decide to use it.

      As others have pointed out, it is always good to be explicit when writing SQL statements. This means writing

      INSERT into the_table(foo, bar, baz) values(...)
      instead of
      INSERT into the_table values(...)
      and
      SELECT foo, bar, baz FROM the_table WHERE ...
      instead of
      SELECT * FROM the_table WHERE ...
      It requires a little more typing, but it clarifies things, and will make errors more obvious.

      Michael

        Thanks for the explanation mpeppler, that was helpful, and points well taken. And hardburn was right, no code samples really necessary, but your examples didn't hurt .

        —Brad
        "A little yeast leavens the whole dough."

      Don't see what code samples could be provided besides what is already above. It's just as mrpeppler said: INSERT INTO foo SET . . . works on MySQL, but may not work anywhere else (it doesn't on PostgreSQL, for instance). Which is unfortunate, IMHO, because the SET syntax makes it easier to figure out which parameters are being set to what in a large statement.

      ----
      I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
      -- Schemer

      : () { :|:& };:

      Note: All code is untested, unless otherwise stated

        While it may be nice syntactic sugar, it isn't portable SQL. And, I don't see how the following is difficult to work with ...
        INSERT INTO foo_table ( foo, bar, baz, foo1, bar1, baz1 ) VALUES ( ?, ?, ?, ?, ?, ? )

        It's all about how you format your SQL. Personally, I format my SELECT statements as such:

        SELECT xx.foo ,xx.bar ,yy.baz ,COUNT(*) AS count FROM foo_table xx ,bar_table yy WHERE xx.abcd = yy.abcd AND xx.asdfghjkl = yy.asdfghjkl GROUP BY xx.foo ,yy.bar ,yy.baz

        Notice where the commas and where the equals signs are. Also, note how I line up the whitespace after the upper-cased keyword. The more you can say in your formatting, the better.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://321833]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 04:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found