Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Access SQL Query from Perl using times with colons

Posted on 2010-11-26
10
Medium Priority
?
348 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:Justin Mathews
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:Justin Mathews
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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:Justin Mathews
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

782 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