in reply to mysql and postgresql gotchas and tips

Why use $dbh->quote when you can just bind the parameters?

my $sth = $dbh->prepare( "INSERT INTO table VALUES ( ?, ?, ?, ? +)" ); $sth->execute( 'a', 1234, undef, 't' ); ## samething for do

To me that looks much much cleaner than just embedding the parameters using quote()

I guess this thing about insert statements doean't bother me because PostgreSQL is the first real database that I had to deal with.... :-)

Replies are listed 'Best First'.
Re: Re: mysql and postgresql gotchas and tips
by ralphie (Friar) on Aug 03, 2001 at 15:24 UTC
    to tell the truth, i just hadn't tried that. sometimes juggling a number of balls means you can't pull one out of the air and look at it more closely <grin>. regardless, a good post for the thread, precisely the kind of thing i was hoping to accumulate.
Re: Re: mysql and postgresql gotchas and tips
by jepri (Parson) on Aug 03, 2001 at 20:48 UTC
    Yupski. I tend to find that PostgreSQL comes back with less than useful error messages sometimes, so if you aren't using placeholders (I did find one case where I couldn't) you should be doing a:

    my $sql = "INSERT INTO $table VALUES ...."; print STDERR "About to execute the sql statement $sql\n" if $debug; my $sth = prepare($sql); ...etc...

    That's pretty obvious but it wasn't instinctive to me until about halfway through my database program.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      Note that interpolating the sql first becomes exactly the same as the original, in that you need to use quote again.

      I'm curious what the case where you can't bind the value is? The worst I've hit was having to use :1 :2 instead of ? ?
      --
      Snazzy tagline here

        Exactly as in my example above, you can't use placeholders for table names. So I couldn't loop over multiple tables at the same time.

        I probably shouldn't have been doing that, but I had to, so I had to find a way.

        ____________________
        Jeremy
        I didn't believe in evil until I dated it.