Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Access SQL Query from Perl using times with colons

Posted on 2010-11-26
10
Medium Priority
?
342 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: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
Technology Partners: 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: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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

688 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