Solved

Access SQL Query from Perl using times with colons

Posted on 2010-11-26
10
338 Views
Last Modified: 2012-05-10
I have an sql query that I am trying to run in Perl. I have data stored every 6 hours and am trying to pull the data from a run 6 hours before the most up to date run

 
$sqlstatement = "Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ";

Open in new window


The #6:00# is to subtract 6 hours from the most recent executed time. I wrote this query in VBA and it works perfectly. However, in perl, the : and # causes problems because Perl is trying to read this in as a parameter. How do I get perl to ignore those symbols and run the SQL query as typed.

If i remove the - #6:00# the query works fine.
0
Comment
Question by:kyle972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 16

Expert Comment

by:jmatix
ID: 34218175
Try putting the SQL in single quotes as:

$sqlstatement = 'Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ';
0
 

Author Comment

by:kyle972
ID: 34218357
I tried single quotes and am still getting an error.

Anything else?
0
 
LVL 16

Expert Comment

by:jmatix
ID: 34218550
Are you using Access ODBC driver? It tried a similar query and it worked fine. Here is the sample. replace with your database path and database name:


#! c:\perl\bin\perl

use Win32::ODBC;

$DriverType = "Microsoft Access Driver (*.mdb)";
$DSN = "Win32 ODBC";
$Dir = "C:\\DB\\acc";
$DBase = "Database11.mdb";


Win32::ODBC::ConfigDSN(ODBC_ADD_DSN, $DriverType,("DSN=$DSN", "Description=Win32 ODBC Test DSN for Perl", "DBQ=$Dir\\$DBase", "DEFAULTDIR=$Dir", "UID=", "PWD=")) or die "ConfigDSN(): Could not add temporary DSN" . Win32::ODBC::Error();

$db=new Win32::ODBC($DSN) or die "couldn't ODBC $DSN because ", Win32::ODBC::Error(), "\n";

$sqlstatement = "Select Location FROM Forecasts WHERE RunExecuted=(SELECT max(RunExecuted) - #6:00# from Forecasts) ";
!$db->Sql($sqlstatement) or die "couldn't do $query because ", $db->Error(), "\n";

while($db->FetchRow())
{
 my %Data = $db->DataHash();
 foreach my $key(keys(%Data)){
   print $key," -> ",$Data{$key};
 }  
}


Win32::ODBC::ConfigDSN(ODBC_REMOVE_DSN, $DriverType, "DSN=$DSN") or die "ConfigDSN(): Could not remove temporary DSN because ", Win32::ODBC::Error();

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:kyle972
ID: 34218879
I am using DBI.

I am now curious how to just use a colon in a normal text string that you are passing to another function.

Example:

$Var="Location : Test"

Function($Var)

The colon will cause the function to not work properly. How would I pass the text string to the function and have it remain as one long text string.

This is the solution to my sql problem as well, but is a much simpler question.



0
 
LVL 16

Expert Comment

by:jmatix
ID: 34218955
As far as I know colon (:) does not have any special meaning in Perl and need not be escaped in a string. For example, try this and it will print the value fine:

$var="Location : Test";
test ($var);
exit;

sub test
{
  my $param = shift;
  print $param;
}

0
 

Author Comment

by:kyle972
ID: 34218987

   Here is an example. I am reading the contents out of a txt file and then sending an email with the contents.

If there is a : in the text file it will cause issues.


my $mailer = new Net::SMTP::TLS(  
       'smtp.gmail.com',  
       Hello   =>      'smtp.gmail.com',  
       Port    =>      587,  
       User    =>      'xxx',  
       Password=>      'xxx';  
   $mailer->mail('xxx');  
   $mailer->to('xxx');  
   $mailer->data;  
   $mailer->datasend("Subject: ");
   
   open (MYFILE, 'Export.txt'); 
while (<MYFILE>) { 
 	chomp;
 	$string=$string."$_\n" ;
	 
	 }


   $mailer->datasend($string);  
   $mailer->dataend;  
   $mailer->quit;

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 34220004
If you're having a problem with sending the email, then try a different mail module.  I prefer to use MIME::Lite for most of my email needs.

#!/usr/bin/perl

use strict;
use warnings;
use MIME::Lite;

open my $fh, '<', 'Export.txt' or die "failed to open 'Export.txt' $!";

$/ = undef;
my $file_contents = <$fh>;
close $fh;

MIME::Lite->send('smtp', 'smtp.gmail.com',
          Timeout  => 60,
          Port     => 587,
          AuthUser => 'xxx',
          AuthPass => 'xxx'
);

my $msg = MIME::Lite->new(
        From     => 'me@myhost.com',
        To       => 'you@yourhost.com',
        Subject  => 'Your Subject',
        Data     => $file_contents,
    );

$msg->send;

Open in new window

0
 
LVL 28

Expert Comment

by:FishMonger
ID: 34220069
Your initial question was related to your sql statement, then you switched to having an email issue.  I don't know which problem you're experiencing, but for the sql issue, have you tried using one of DBI's quote methods?

http://search.cpan.org/~timb/DBI-1.615/DBI.pm#quote
0
 

Accepted Solution

by:
kyle972 earned 0 total points
ID: 34444721
I accomplished the same thing by subtracting .25. Using .25 instead of #6:00# corrected the problem.
0
 

Author Closing Comment

by:kyle972
ID: 34463332
It works
0

Featured Post

Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question