Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: DBI Quoting question

by btrott (Parson)
on May 10, 2000 at 20:46 UTC ( [id://11002]=note: print w/replies, xml ) Need Help??


in reply to DBI Quoting question

Use placeholders--then the values will automatically be quoted.
my $NUM_FIELDS = 14; my $sql = "INSERT INTO whatever VALUES (" . join(', ', ('?') x $NU +M_FIELDS) . ")"; my $sth = $dbh->prepare_cached($sql); while (<FILE>) { my @line = (parse_cvs($_)); $sth->execute(@line); } $sth->finish;
If you want to be more flexible about the number of fields in your file, do the prepare inside the loop (but make sure you use prepare_cached so that you're not preparing the same statement over and over):
while (<FILE>) { my @line = (parse_cvs($_)); my $sql = "INSERT INTO whatever VALUES (" . join(', ', ('?') x @line) . ")"; my $sth = $dbh->prepare_cached($sql); $sth->execute(@line); $sth->finish; }
In fact, I like this last better. :) I'd recommend using this, because it's nice to be input-independent.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-23 18:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found