Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

(dkubb) Re: (1) Using CGI.pm params to make a DBI MySQL query

by dkubb (Deacon)
on Oct 10, 2001 at 06:57 UTC ( [id://117919]=note: print w/replies, xml ) Need Help??


in reply to Using CGI.pm params to make a DBI MySQL query

I have one comment that hasn't been addressed yet, is slightly OT, but still important.

In your SQL query you may want to explicitly specify the columns you are inserting into. This has a large benefit: if you add a column to the table or the column ordering changes, your code is less likely to break. A good rule of thumb I use is to:

  • never assume the order of columns in an SQL query
  • always explicitly define the columns

I've found this to be especially helpful in SELECT and INSERT -type statements.

Here's an example that demonstrates what I mean and answers your question:

$dbh->do( 'INSERT INTO table (Col1, Col2, Col3) VALUES (?, ?, ?)', {}, map scalar param($_), qw(Col1 Col2 Col3), );

I began doing this after thinking about the issues raised in Topics in Perl Programming: Table-Mutation Tolerant Database Fetches with DBI, and realizing the way I was doing SQL does not lend itself to change very well.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-20 01:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found