rbc has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I have am writing a script that will create sqlldr control
files for me. I am having trouble printing 0.
I am sure this a common problem that you wise monks have
seen before and have overcome with easy.
Here's my script:
|All the time
... should of been outputted as ...
0|All the time
As per this query below ...
I have am writing a script that will create sqlldr control
files for me. I am having trouble printing 0.
I am sure this a common problem that you wise monks have
seen before and have overcome with easy.
Here's my script:
And here is a example of the bug:#!/usr/bin/perl -w use strict; use DBI; my $table = shift; my $dbuser = shift; my $dbpass = shift; my $dbname = shift; my $dbd = 'Oracle'; my $dbh = DBI->connect ( $dbname, $dbuser, $dbpass, $dbd); if (!$dbh) { die "$0 Error conecting to DB ($dbuser/$dbpass\@$dbname); $DBI::er +rstr\n"; } my $SQL = <<SQL; Select * from $table SQL $dbh->{LongReadLen} = 65500; my $sth = $dbh->prepare($SQL) || die $dbh->errstr; $sth->execute(); my $colNames = $sth->{'NAME'}; my $nColumns = $sth->{'NUM_OF_FIELDS'}; print <<DOIT; OPTIONS (ROWS=1) LOAD DATA INFILE * REPLACE INTO TABLE $table FIELDS TERMINATED BY '|' TRAILING NULLCOLS ( DOIT my $colString; for my $colName ( @$colNames ) { $colString .= "\n\t$colName,"; } chop($colString); print "$colString\n"; print<<DOIT; ) BEGINDATA DOIT while ( my @r = $sth->fetchrow_array ) { # print join ( '|', @r ); # This causes another bug of putting 0's where # I wanted || outputted :( # print join ( '|', map ( $_ || '0', trim( @r )) ); # print join ( '|', map ( $_, trim( @r )) ); print "\n"; } $sth->finish || die; $dbh->disconnect; sub trim { my @out = @_; if ( !defined(@_)) {return "";} if ( $#out == -1 ) {return "";} for(@out) { #next if !($_); if (!($_)) { $_ = ""; } s/^\s+//; s/\s+$//; s/\|//g; s/\n//g; s/\cM//g; s/\r//g; } return wantarray ? @out : $out[0]; }
OPTIONS (ROWS=1) LOAD DATA INFILE * REPLACE INTO TABLE DECODE_TABLE FIELDS TERMINATED BY '|' TRAILING NULLCOLS ( VALUE, DESCRIPTION ) BEGINDATA 1|Something 2|Something else 3|Sometimes 4|Never 5|Always 6|Only once in a while 7|Once in a bluemoon |All the timeNote the line ...
|All the time
... should of been outputted as ...
0|All the time
As per this query below ...
$ sqlplus blah... SQL> select * from DVAL_ADR_AVAIL 2 / V DESCRIPTION - ------------------------------ 1 Something 2 Something else 3 Sometimes 4 Never 5 Always 6 Only once in a while 7 Once in a bluemoon 0 All the time 8 rows selected.Please help! Thanks :)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: getting 0 to print
by RMGir (Prior) on Jan 13, 2003 at 20:39 UTC | |
by rbc (Curate) on Jan 13, 2003 at 20:51 UTC | |
by pfaut (Priest) on Jan 13, 2003 at 21:40 UTC | |
by rbc (Curate) on Jan 13, 2003 at 21:53 UTC | |
Re: getting 0 to print
by dws (Chancellor) on Jan 13, 2003 at 20:41 UTC | |
| |
Re: getting 0 to print
by Arien (Pilgrim) on Jan 13, 2003 at 23:21 UTC | |
by rbc (Curate) on Jan 13, 2003 at 23:32 UTC | |
by Arien (Pilgrim) on Jan 14, 2003 at 00:08 UTC | |
Re: getting 0 to print
by Enlil (Parson) on Jan 13, 2003 at 20:43 UTC |
Back to
Seekers of Perl Wisdom