[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] TMTOWTDI: SQL creation




All this talk of how to write stuff makes me smile, 'cause I often roll
around in my head the various ways to do things.  There's certainly no
right or wrong way, and sometimes going back to a C-like method is the
best.

I'm writing some code that happens to need to build some SQL on the fly,
based on some field names and values.  I originally wrote it like:

        my $sql =
            "INSERT INTO " . $parms{$table} .
            "(" . join( ",", @fieldnames ) . ")" .
            "VALUES ( " . join( ",", @values ) . ")";

But I think that's so ugly, and so hard to follow, so I rewrote like:

        my $sql =
            sprintf( "INSERT INTO %s ( %s ) VALUES ( %s )",
                $parms{$table},
                join( ",", @fieldnames ),
                join( ",", @values );

Sure, sprintf() may be Old Skool, but in this case, I think it makes
things far clearer.

xoxo,
Andy

-- 
Andy Lester: andy@petdance.com  http://petdance.com/
"It takes a teenage riot to get me out of bed today."



==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org