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
Solved

Access SQL Query from Perl using times with colons

Posted on 2010-11-26
10
329 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
  • 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

837 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