Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Using placeholders in MySQL returning an error

by hardburn (Abbot)
on Jan 16, 2004 at 16:56 UTC ( [id://321829]=note: print w/replies, xml ) Need Help??


in reply to Using placeholders in MySQL returning an error

. . . mentions the placeholder for an auto incre. id is not necessary (however, I have found in other scripts this was necessary--weird).

The node in question was correct. Not only is setting an auto increment field unnecessary, it's undesirable without a really good reason. I suspect the other scripts you mentioned were using a database with a bad schema.

$sth = $dbh->prepare (q{ INSERT INTO sponsor VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)} );

After you call prepare, add a or die "Couldn't prepare statement: " . $dbh->errstr;, or turn RaiseError on when you call DBI->connect (unless you already have). A similar note goes for the execute (which can fail if you have an incorrect number of parameters).

You have a huge number of placeholders there. Instead of a static SQL statement, you can generate the SQL at runtime to ensure you have the correct number of placeholders for your data. This can be done by pulling all the parameters you set out of execute statement and into a hash. The keys of the hash are the name of the fields, and the values are the data you put in for that field. Example:

my %sql_data = ( name => $name, address => $address, # and so on ); my $sql = q/INSERT INTO sponsor (/ . join(',', keys %sql_data) . q/) VALUES (/ . join(',', ('?') x keys %sql_data) . q/)/; my $sth = $dbh->prepare($sql) or die $dbh->errstr; # values() is guarenteed to come out in the same order as keys() $sth->execute(values %sql_data) or die $dbh->errstr; $sth->finish();

----
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

Replies are listed 'Best First'.
Re: Re: Using placeholders in MySQL returning an error
by Grygonos (Chaplain) on Jan 16, 2004 at 18:57 UTC

    On a note of maintinability/style.. it's better to use complete inserts i.e.
     INSERT INTO tablex (field1, field2, field3) VALUES ('x','y','z')
    over incomplete inserts i.e.
     INSERT INTO tablex('x','y','z')
    Performing the following:
     ALTER TABLE tablex ADD field4 varchar(20)
    breaks your incomplete insert, since you are not specifying directly which fields the values being inserted are mapped to, it expects your insert to include a value for field4.

    If you have an app that uses this table but will never make use of the new field: field4, then you have no need to alter that app. Leaving incomplete inserts lying around in a script is a recipe for disaster.

    p.s. I learned this the hard way.

    Grygonos
Re: Re: Using placeholders in MySQL returning an error
by bradcathey (Prior) on Jan 16, 2004 at 19:44 UTC
    Amazing hardburn. I tried the hash and bingo. Gonna love this solution for those huge tables. You don't know how many times I counted those ?'s to make sure I had the right number. And they look kinda funny all strung out in the code. Anyway, elegant solution. Thanks.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-25 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found