Hello Monks,
I'm seeking your wisdom. I'm not familiar with DBI but I have a task that I need to accomplish. I'm trying to connect to a database and query a table. Then the output put it into a file with a comma delimiter. I'm passing a variable to the subroutine but it looks like is not querying anything. the code runs without any errors or warnings but It doesn't create an output. Could you take a look at the code and let me know what do I'm doing wrong to modify it and make it work. I will appreciate any suggestions. Thanks in advance.
Here is the code:
#! perl -w
use strict;
use Win32::ODBC;
my $dir = 'K:\\reports';
my $count = 0;
opendir DH, $dir or die "Cannot open $dir: $!";
while (my $file = readdir DH) {
next unless $file =~ /\.pdf$/;
my $newfile = substr($file,2,6);
process_records($newfile);
}
closedir DH;
sub process_records {
my ($newfile) = @_;
my $dbname = 'dbname';
my $username = 'account';
my $password = 'password';
my ($db);
if (!ref($db = new Win32::ODBC("DSN=$dbname;UID=$username;PWD=$passwo
+rd"))) {
my ($ErrNum, $ErrText, $ErrConn) = Win32::ODBC::Error();
#...IT FAILED - DO SOMETHING...
}
my $sql = "SELECT field1#,field2#,field3#,field4#,field5#,field6,lastn
+ame,firstname
FROM dbo.table
WHERE field2# = $newfile";
my ( $yr, $mo, $dy ) = (localtime)[5,4,3];
my $outfile = sprintf( "C:\\Directory\\%04d%02d%02d.txt",$yr+1900,$mo+
+1,$dy );
open OUT,">$outfile" or die "Couldn't open $outfile, $!";
print OUT "$sql\n";
if($db->Sql($sql)){
#...FAILED...
my ($ErrNum, $ErrText, $ErrConn) = $db->Error();
}
while($db->FetchRow()){
my %data = $db->DataHash();
#...Processing Data...
}
$db->Close();
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.