The way you're doing things, you're not using placeholders. You're hard coding the values into the SQL string (which is bad, for many reasons). Unless all of the values are numbers, or already quoted and escaped, it'll fail horribly.

I'd suggest the following (and as with BrentDax, I'd recommend looking at the string you're creating when you have errors).

my @fields = sort keys %prophash; my $sql = 'INSERT INTO maintable ('.join(',',@fields) .') values ('.join(',',('?')x(scalar @fields)).')'; my $sth = $dbh->prepare($sql) or die "Can't prepare statement\n\n$sql\n\n$dbh->errstr()"; $sth->execute(@prophash{@fields}) or die "Can't execute statement : $db->errstr()";

Yes, the sort may seem extraneous, but it'll make it so your sql statements are more likely to be consistent between systems, and so have its execution plan cached.


In reply to Re: large number of placeholders by jhourcle
in thread large number of placeholders by Anonymous Monk

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.