Re: Exporting data from tables into a CSV delimited file

Re: Exporting data from tables into a CSV delimited file

 

  

Hello tboss,

Tuesday, August 3, 2004, 3:15:44 PM, you wrote:
t> I have a perl-based solution, if you like perl. And who doesn't
t> like perl! :-)

I like perl, but would change some things:
1. parsing commandline parameters
2. remove " $sql = "select ";"
3. add "or die" expressions to locate errors
4. "use strict" and use my for variables.
5. open and close OUT before and after while loop

-- 8< --
#!/usr/local/bin/perl

use strict;
use DBI;

if ($ARGV[0] =~ /(.*)\/(.*)\@(.*)/) {
} else {
print 'Usage: cvsdump.pl user/[Email Address Removed] tablename\n';
exit(1);
}

my $dbh = DBI->connect ("DBI:Oracle:$3", $1, $2) or die "Can't connect";
my @row;
my $sql = "select * from $ARGV[1]";
my $sth = $dbh->prepare($sql) or die "prepare $sql";
$sth->execute or die "execute";

open(OUT, ">$ARGV[1].csv") or die "Can't open $ARGV[1].csv";

while (@row = $sth->fetchrow_array) {
my $count=@row;
for (my $i=0;$i<$count;$i++) {
print OUT "'$row[$i]'";
print OUT "," unless ($i == $count-1);
}
printf OUT "\n";
}

close(OUT);

$sth->finish or die "finish";
$dbh->disconnect or die "disconnect";
-- 8< --


cu
Wieland mailto:[Email address protected]


Oracle LazyDBA home page