Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

storing queries in an array and execute

by kaka_2 (Sexton)
on Nov 25, 2016 at 09:23 UTC ( [id://1176528]=perlquestion: print w/replies, xml ) Need Help??

kaka_2 has asked for the wisdom of the Perl Monks concerning the following question:

I am preparing a script using which i would like to execute mysql query. I am already having access to local node so I can use mysql –e “Query” DBNAME. What I am trying to do is following.

my $Query1 = "mysql -e \"SELECT count(id_) FROM jbpm_taskinstance WHER +E isopen_ IS TRUE AND actorid_ IS NOT NULL\" DB1"; my $Query2 = "mysql -e \"SELECT count(id_) FROM jbpm_taskinstance WHER +E create_>(SELECT DATE_ADD(NOW(), INTERVAL '-3' HOUR))\" DB1"; my @QXXXX = (\$Query1,\$Query2); foreach my $q (@QXXXX) { $out = qx(Dumper($q)); print $out . "\n"; }

but when executing I get error.

sh: -c: line 0: syntax error near unexpected token `SCALAR'

sh: -c: line 0: `Dumper(SCALAR(0x307ef60))'

sh: -c: line 0: syntax error near unexpected token `SCALAR'

sh: -c: line 0: `Dumper(SCALAR(0x307ef70))'

Could someone help me in fixing it?

Replies are listed 'Best First'.
Re: storing queries in an array and execute
by hippo (Bishop) on Nov 25, 2016 at 11:43 UTC

    Using DBI instead of shelling out will save your sanity and also that of anyone else who has to maintain your code. Here is an untested example to give you the gist:

    #!/usr/bin/env perl use strict; use warnings; use DBI; my $dbh = DBI->connect ('DBI:mysql:database=DB1') or die $DBI::errstr; my $basequery = 'SELECT count(id_) FROM jbpm_taskinstance'; my @where = ( q#WHERE isopen_ IS TRUE AND actorid_ IS NOT NULL#, q#WHERE create_>(SELECT DATE_ADD(NOW(), INTERVAL '-3' HOUR))#, ); for my $thiswhere (@where) { my ($qty) = $dbh->selectrow_array ("$basequery $thiswhere"); print "Count $thiswhere is $qty\n"; } $dbh->disconnect;
Re: storing queries in an array and execute
by choroba (Cardinal) on Nov 25, 2016 at 09:41 UTC
    Update: Sorry, I misread the question. Just remove the `qx`, it tries to run Dumper through the shell.
    use Data::Dumper; $out = Dumper($q);

    Old and wrong answer: It seems you're calling the script as a shell script. Either, call it with perl :

    perl myscript.pl

    or prepend the shebang line to the top of the script to tell your system what interpreter to use:

    #!/usr/bin/perl

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      if i remove the qx then it is just printing the queries whereas i want to execute These queries.
        Ah, then keep the qx and remove the Dumper.

        But you should use DBI with DBD::mysql instead of shelling out.

        ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1176528]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-29 11:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found