#! 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 = 'DB_Name';
my $username = 'Username';
my $password = 'Password';
my ($db);
if (!ref($db = new Win32::ODBC("DSN=$dbname;UID=$username;PWD=$password"))) {
my ($ErrNum, $ErrText, $ErrConn) = Win32::ODBC::Error();
#...IT FAILED - DO SOMETHING...
print "couldn't connect";
}
my $sql = ("SELECT field1,field2,field3,field4,field5,field6,lastname,firstname
FROM db_name.table
WHERE field1 = '029622'");
if($db->Sql($sql)){
#...FAILED...
my ($ErrNum, $ErrText, $ErrConn) = $db->Error();
}
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, $!";
while($db->FetchRow()){
my %data = $db->DataHash();
#...Processing Data...
print OUT "%data\n";
}
$db->Close();
# }
#!perl -w
use strict;
use DBI;
my $dsn = "some system DSN or DSN-less connection";
my $dbh = DBI->connect(
"dbi:ODBC:$dsn",'me','mypass',{RaiseError=>1}
);
# ...
Unless you have a very specific reason to use Win32::ODBC, use DBI instead - there is more documentation and a larger community to provide support.
I'm geting stuck because I don't have enough experience with DBI. ... I have not seen many samples of reference points in DBI
To fix that lack of knowledge, have a look at the Tutorials, see the Working with Files and Databases section especially:
I have heard many monks sing in praise of the node 284436.
Cheers, Sören
I rejected DBD::Pg and DBD::PgPP, which I think means that I rejected DBI (which is described an alternative to ODBC though DBD::ODBC requires the DBI module...I'm just accepting that stuff for now). DBD::Pg was rejected because I heard of a monk who couldn't get the current version working on Windows, and I heard elsewhere that an old version of MSVC is required, and I don't want to be limited to an old version of anything. DBD::PgPP was suggested, but I don't want to use a non-core .05 version of a module, especially when it has so many non-core dependencies. That's when ODBC was suggested.
I'm going to (attempt to) use DBD::ODBC instead of Win32::ODBC because I want more portability.
Semi-off-topic observation: When software doesn't have a simple installer, it's because it's not known how to install it, and expect the documentation to suck more than usual.
Fully off-topic observation: Much of the Perl documentation sucks (details available upon request). I heard that there is excellent Python Documentation. Nuff said.
perlmonks.org content © perlmonks.org and Anonymous Monk, Happy-the-monk, jZed, McMahon, periapt, Wassercrats
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03