Hello Monks! I have been modifying an existing Perl script to add some database inserts and updates. I configured the changes I needed, then set about trying to make some it more efficient and organized for the next person. This is a basic example, but I have an insert command that will get called dozens of times in a script:
my $sql_ins_log = "insert into download_log (download_id,status,update +_time)" . " values(?,getValueByMeaning('$status_meaning','17') +,?)";
I have this inside of a for loop, but the sql itself should be the same, with only the variables changing. So I thought, well I can just move that getValueByMeaning() function as a parameter (like the download_id and update_time) and set the $sql_ins_log outside of the for loop to avoid setting the variable over and over to the same thing, like this:
my $sql_ins_log = "insert into download_log (download_id,status,update +_time)" . " values(?,?,?)"; foreach(@X){ $now = localtime->mysql_datetime; $sth = $dbh->prepare($sql_ins_log); $sth->execute($download_id,getValueByMeaning('$status_meaning','17') +,$now); }
I tried a variety of things, but I'm having issues that I think relates to the fact that it's a function and there's a variable in it (meaning, it's not just a function like NOW()). To be clear, the getValueByMeaning is a user defined MySQL function, not a Perl function in this script. So I want the actual SQL to get passed into MySQL like this:
insert into download_log (download_id,status,update_time) values(1,get +ValueByMeaning('DONE',17'),'2014-09-15 14:16:00')
Thanks for ay insight!

In reply to MySQL Parameterized Query with Functions by hoyt

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.