in reply to perl and microsoft sql server
This script was a quick hack (which explains the poor commenting), but should give you a nice starting point for writing your own:fieldName{tab}value
Hope this helps!#!c:\perl\bin -w use strict; use DBI; my @data; my $DSN = 'somedsn'; my $USER = 'someuser'; my $PASSWORD = 'somepass'; # Here we will store files with bad records my %badrecs; # Connect to database my $dbh = DBI->connect("dbi:ODBC:$DSN", $USER, $PASSWORD, {RaiseError => 1}) or die "Couldn't connect to database: " . DBI->errstr; my $dir = "G:/local/common/rates/"; # Get all filenames opendir(DIR, $dir) || die "Can't opendir $dir: $!"; my @files = readdir(DIR); closedir DIR; # Read each file and ad to database my $numfiles = $#files; my $i = 0; foreach my $file (@files) { print "Processing " . $i++ . " out of $numfiles files: $file"; # filename must be letters or digits followed by .txt extension if ($file =~ /^[-a-zA-Z0-9]+\.txt/) { my @data = readConfig($file); my $line = 0; PROCESS_RECORD: foreach my $arrayref (@data) { print ' ' . ++$line; for my $j (0..3) { # We'll not process record if any of first three value +s are false # Third ($j == 2) value must be numeric # Fourth value is not required, but if it exists, it m +ust be numeric if (($j < 3 && !@{$arrayref}[$j]) || ($j == 2 && @{$arrayref}[$j] !~ /^[0-9.]+$/) || ($j == 3 && defined @{$arrayref}[$j] && @{$arrayre +f}[$j] !~ /^[0-9.]+$/)) { $badrecs{$file} .= $line . ','; next PROCESS_RECORD; } } insertConfig(@{$arrayref}); } } else { print " Skipped"; } print "\n"; } # Disconnect from database $dbh->disconnect; open BADRECS, ">BadRecs.txt" or die "Can't open BadRecs for writing: $ +!\n"; for (keys %badrecs) { $badrecs{$_} =~ s/,$//; print BADRECS $_ . "\t" . $badrecs{$_} . "\n"; } close BADRECS; exit(); sub readConfig { my $fto = shift; $fto =~ /([^.]+)\.txt/; my $lenderID = $1; open(CONFIG, "<$dir$fto") or die "Can't open $dir$fto: $!"; # my ($lenderID,$progName,$rate,$apr,$fee); my (@rows); while(<CONFIG>) { chomp; my @value = ($lenderID, split(/\t/)); push @rows, \@value; } close(CONFIG); return @rows; } sub insertConfig { my @value = @_; my @fieldName = qw(lenderID progName rate apr fee); my ($sql, $fields, $placeHolders); for (0..$#value) { $fields .= $fieldName[$_] . ','; $placeHolders .= '?' . ','; } $fields =~ s/,$//; $placeHolders =~ s/,$//; $sql = 'INSERT INTO lenderrecs.dbo.rates (' . $fields . ') VALUES +(' . $placeHolders . ')'; my $sth = $dbh->prepare($sql); $sth->execute(@value); }
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just go the the link and check out our stats.
|
|---|