Hello monks. I am relatively new to the DBI module and i ve come up to an obstacle so i need your wisdom :) There is a case, where i need to prepare a mysql query, for inserting data to a table, which contains some dozens of columns. I use bind_params for the preparation, but the thing is that sometimes the query itself must change and use some mysql native function like 'NOW()'. For better clearance of code and for better maintenance, i 've decided to go with a hash, using the mysql column names as its keys, and the values as its elements, so the code is in a smaller scale like this:
my %test = ( "`test`.`id`" => "NULL", "`test`.`hash`" => "MD5()", "`test`.`power`" => "HASH()", "`test`.`allegiance`" => "?", ); my $str = join(", ", map { "$_" } keys %test); my $val = join(", ", values %test); my $query = "INSERT INTO `test` ($str) VALUES ($val)";
from what i've seen till now, it works, but i don't know if it is from some divine intervention or something else, because i am not really sure, if $val will always have the order of the values in the corresponding order of the keys in the $str. Moreover, since this is the best i can think of, and i am sure someone has come with something better, can you suggest anything else for formatting an sql insertion query for a table of 60+ columns? What i really care about is the ease of read for the code. Thank you for your time :)

In reply to DBI and mysql query formatting by kimlid2810

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.