Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Sorry for asking possibly stupid questions, but why use a complicated library for something like this when you can just write the query directly and insert safed values? Something like this:

use strict; use warnings; ### Matches negatives, decimals sub isNum { return $_[0] =~ m/^-?(?:\d*\.)?\d+$/; } ### Converts non-safe characters in string sub safeString { my $val = $_[0]; $val =~ s/(['"\\\x00\x1a])/\\$1/g; return $val; } ### Determines type of value, converts string sub safeValue { my $val = $_[0]; return "'" . safeString($val) . "'" if length($val) == 0 || !isNum($val); return $val; } ### Safes all inserted values ### Inserts strings with quotes if {key} is used ### Inserts without quotes if [key] is used sub queryMysql { my ($query, $args) = @_; $query =~ s/\[(\w+)\]/safeString($args->{$1})/eg; $query =~ s/{(\w+)}/safeValue($args->{$1})/eg; return $query; } my %values = ( 'table' => 'tablename', 'x' => '123', 'y' => 'alpha', 'z' => -45.67, 'colname' => 'COLB', 'from' => '20110101', 'to' => '20110131' ); print queryMysql(' UPDATE [table] SET x = {x}, y = {y}, z = {z} WHERE [colname] BETWEEN {from} AND {to}', \%values); ### UPDATE tablename ### SET x = 123, y = 'alpha', z = -45.67 ### WHERE COLB BETWEEN 20110101 AND 20110131
I hacked this together years ago for situations where I don't want to bother messing with the ? ? and separate array format of DBI. Could probably take a few minutes and write a function for generating inserts and updates as well.

In reply to Re: SQL::Abstract 'between' clause (about done) by TJPride
in thread SQL::Abstract 'between' clause (about done) by Anonymous Monk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2019-01-16 04:24 GMT
    Find Nodes?
      Voting Booth?
      After Perl5, I'm mostly interested in:

      Results (274 votes). Check out past polls.