[Denver-pm] Better way to handle vars?
Chris Fedde
chris at fedde.us
Tue Jul 1 19:06:31 PDT 2014
SQL is just text so generating it is pretty easy. For insert statements I
sometimes craft lines like this:
use Modern::Perl;
my @fields = qw(TIMESTAMP KEY VALUE);
my $insert = "insert into table ("
. join(", ", @fields)
. ") values ("
. join( ", ", ("?")x at fields)
. ")";
say $insert
There are also LOADS of SQL modules on CPAN.
One of my favorite for mucking about with plain old SQL is SQL::Abstract
use Modern::Perl;
use SQL::Abstract;
my $sql = SQL::Abstract->new;
my %fields = (
TIMESTAMP => scalar gmtime,
KEY => rand(),
VALUE => "some value",
);
my ($insert, @bind) = $sql->insert('table', \%fields);
say $insert. "\n";
say for (@bind);
On Tue, Jul 1, 2014 at 4:49 PM, deandre <deandre at deandrecarroll.com> wrote:
> If I understand your problem correctly, this is the type of work for which
> prepared statements are created.
>
>
> $ sth = $ dbh-> prepare ("insert into table ( TIMESTAMP, KEY, VALUE )
> values ( ?,?,? )";
>
> $ sth-> execute($Timestamp, $Key, $Value);
>
> Values passed to execute are escaped, thus preventing possible sales
> injection. Plus the statement handle goes through a sort of compilation
> making repeated use (by adding new values to the execute) much faster.
>
> Sent on a Sprint Samsung Galaxy S® III
>
>
> -------- Original message --------
> From: "Robert L. Harris"
> Date:07/01/2014 4:22 PM (GMT-07:00)
> To: Denver-pm at pm.org
> Subject: [Denver-pm] Better way to handle vars?
>
>
> OK, so I'm parsing a ton of data based on patterns in the lines. Given
> the line I pull out 3 things,
> Timestamp
> Key
> Value
>
> Right now for one of my patters I have
>
> $Insert="insert into table ( TIMESTAMP, KEY, VALUE ) values ( $Timestamp,
> $Key, $Value )";
>
> ( effectively ).
>
> I'm going to have about 20+ of these patterns. The actual insert is about
> 5x longer. Is there a way to "template" the Insert so I can basically
> reference the template in the pattern and re-use the template? That way if
> the template has to change down the line ( a new field added ) I update the
> template and not 20+ variables?
>
> Robert
>
>
> --
> :wq!
> ---------------------------------------------------------------------------
> Robert L. Harris
>
> DISCLAIMER:
> These are MY OPINIONS With Dreams To Be A King,
> ALONE. I speak for First One Should Be A Man
> no-one else. - Manowar
>
> _______________________________________________
> Denver-pm mailing list
> Denver-pm at pm.org
> http://mail.pm.org/mailman/listinfo/denver-pm
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/denver-pm/attachments/20140701/43b46173/attachment.html>
More information about the Denver-pm
mailing list