Hello Monks, I'm trying to automate a process but I'm geting stuck because I don't have enough experience with DBI. The code below reads a directory into an array then it passes the file names (numbers) to the other subroutine which opens a session with SQL Server 2000 to extract data. the issue is that I keep getting "Can't call method "SQL" on an undefined value at line 46". I would like the output of the select statement to be dump into a file which is created before it fetches data. Could someone that have experience connecting to databases with perl help me? or point me in the right direction to fix this program. I have not seen many samples of reference points in DBI. I appreciate your help.
#! 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=$passwo +rd"))) { 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(); # }

In reply to Extracting data using WIN32::ODBC by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.