JoeTheProgrammer has asked for the wisdom of the Perl Monks concerning the following question:
I'm relatively new to Perl (as well as programming in general). I'm writing a Perl script to automate our monthly stats. The script below will connect to the Oracle database (Express edition 11g) and get the values from a table.
There's another script that runs everyday and adds stats to this table. So, this table has data for every day. I want to get the overall data for the last month.
When I run this script I don't get any error messages. I just get the '0 results' output. However, when I run the exact same SQL statement in the database directly i get a proper output!
It seems that there's a problem in the way I'm passing the date to the SQL statement. I've tried multiple variations of this but nothing seems to work! :(
Any help on where I'm going wrong will be highly appreciated.
--------use strict; use warnings; use DBI; my $dbh = DBI->connect('DBI:Oracle:SID', 'username', 'password') or di +e "Couldn't connect to database: " . DBI->errstr; # # this date format is just for demo. # the actual variable will have date passed like this: # my $date = "$month/%/$year"; # where $month & $year will be calculated using DateTime # my $date = '08/%/2015'; # I've also tried other date formats like '"08/%/2015"' but nothing + seems to work! :( my $sth = $dbh->prepare('SELECT sum(v1),sum(v2),sum(v3),sum(v4),sum(v5 +) FROM some_table WHERE DATE LIKE ?'); $sth->execute($date) or die "Couldn't get values from table: " . DBI-> +errstr; my ($v1,$v2,$v3,$v4,$v5); if ($sth->rows == 0) { print "0 results\n"; exit; }else{ while (my @data = $sth_1->fetchrow_array()) { $v1 = $data[0]; $v2 = $data[1]; $v3 = $data[2]; $v4 = $data[3]; $v5 = $data[4]; } print "$v1,$v2,$v3,$v4,$v5\n"; }
UPDATE
I've managed to fix this based on suggestions by runrig and SimonPratt. Thanks guys :)
Here's what I did:
1. The dates in the table are stored in Oracle's default date format. So instead of using the 'LIKE' and the % wildcard I used actual dates for searching. The new statement looks like this:
SELECT sum(EMAIL_IRT),sum(DROP_CALL_COUNT),sum(ANS_CALL_COUNT),sum(EMA +ILS_IN),sum(EMAILS_OUT) FROM CS_STATS WHERE DATE_ADDED >= TO_DATE(?,\ +'yyyy-mm-dd\') AND DATE_ADDED <= TO_DATE(?,\'yyyy-mm-dd\')
2. Set the dates between which I want to search using DateTime. The script will run 1st of every month (to get the stats for the previous month) so I'm using this piece of code to get the 1st and last dates.
my $yesterday = DateTime-> now(time_zone=>'Asia/Calcutta')->subtract( +days => 1 ); my $y_month = $yesterday->month; my $y_year = $yesterday->year; if ($y_month < 10){ $y_month = "0".$y_month; } my $finaldt1 = $y_year."-".$y_month."-01"; my $finaldt2 = $yesterday->ymd;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem passing date to SQL
by runrig (Abbot) on Aug 18, 2015 at 15:36 UTC | |
|
Re: Problem passing date to SQL
by GotToBTru (Prior) on Aug 18, 2015 at 15:10 UTC | |
by JoeTheProgrammer (Novice) on Aug 18, 2015 at 15:27 UTC | |
|
Re: Problem passing date to SQL
by kennethk (Abbot) on Aug 18, 2015 at 15:14 UTC | |
by JoeTheProgrammer (Novice) on Aug 18, 2015 at 15:40 UTC | |
by choroba (Cardinal) on Aug 18, 2015 at 15:43 UTC | |
by kennethk (Abbot) on Aug 18, 2015 at 17:53 UTC | |
by GotToBTru (Prior) on Aug 18, 2015 at 16:18 UTC | |
|
Re: Problem passing date to SQL
by poj (Abbot) on Aug 18, 2015 at 17:57 UTC | |
|
Re: Problem passing date to SQL
by SimonPratt (Friar) on Aug 18, 2015 at 15:51 UTC | |
by runrig (Abbot) on Aug 18, 2015 at 20:41 UTC | |
by SimonPratt (Friar) on Aug 18, 2015 at 21:09 UTC | |
|
Re: Problem passing date to SQL
by Hermano23 (Beadle) on Aug 18, 2015 at 15:02 UTC | |
by JoeTheProgrammer (Novice) on Aug 18, 2015 at 15:25 UTC | |
by ravi45722 (Pilgrim) on Aug 19, 2015 at 04:48 UTC | |
by afoken (Chancellor) on Aug 19, 2015 at 18:51 UTC | |
by soonix (Chancellor) on Aug 20, 2015 at 10:39 UTC |