Generating dynamic SQL statements and need to plug in bind values? Maintain a single list of columns and let this function handle stringifications and preping your bind valuees array.
sub setup_bindings { # Produces strings useful for generating dynamic # SQL statements as well as an array of matching # binding values (because you use bind values, right?) #Args: # $_[0]: Ref to hash of values, keyed by column # $_[1]: Ref to array of columns used #Usage: # my ( $column_string, $bindings_string, $bound_values ) # = setup_bindings( \%value_for_column, \@columns ); # my $statement = " INSERT into YOUR_TABLE ( $column_string ) # VALUES ( $bindings_string )"; # $dbhandle->do( $statement, undef, @$bound_values ); #Or whatever +use you have- this is `do` from the DBI my $values = shift; my @columns = @{ +shift }; my @set_columns = (); my @set_bindings = (); my @bound_values = (); for my $col_2_bind ( @columns ) { push @set_columns, $col_2_bind; push @set_bindings, '?'; push @bound_values, $values->{$col_2_bind}; }; my $column_string = join ', ', @set_columns; my $bindings_string = join ', ', @set_bindings; return ($column_string, $bindings_string, \@bound_values); }

In reply to Dynamic SQL Bind values by blogical

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.