export the query into csv file [message #687322] |
Tue, 21 February 2023 14:18 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
I am expecting the query to be output to the csv format. currently, i am getting error in the below code.
can you let me know what is the changes need to be done.
#!/usr/bin/perl
use DBI;
my $dbh = DBI->connect ('dbi:Oracle:projv6t', 'data', 'domi', {PrintError => 1, AutoCommit => 0 });
my $sth = $dbh->prepare (qq{ SELECT data FROM data.QC_PREVIOUS_DATA_VIEW MINUS SELECT data FROM data.QC_CURRENT_DATA_VIEW });
$sth->execute;
open(OUTPUT,">prakash.csv") || die "can not open file";
while(@row = $dbh->fetchrow_array) {
print OUTPUT "tname\n";
}
|
|
|
|
|
|
|
|
|
|
|
Re: export the query into csv file [message #687331 is a reply to message #687330] |
Thu, 23 February 2023 06:27 |
|
arun888
Messages: 100 Registered: June 2015 Location: INDIA
|
Senior Member |
|
|
#!/usr/bin/perl
use DBI;
my $dir = '/data/QC';
my $dbh = DBI->connect ('dbi:Oracle:projv6t', 'data', 'domi', {PrintError => 1, AutoCommit => 0 });
my $sth = $dbh->prepare(qq{SELECT data1, data2, data3 FROM data.QC_CURRENT_DATA_VIEW MINUS SELECT data1, data2, data3 FROM data.QC_PREVIOUS_DATA_VIEW});
$sth->execute;
open(OUTPUT,">$dir/QC_NEW_DATA.csv") || die "can not open file";
while(my @row = $sth->fetchrow_array) {
print OUTPUT join ("\t", @row), "\n";
}
my $sth = $dbh->prepare(qq{SELECT data1, data2, data3 FROM data.QC_PREVIOUS_DATA_VIEW MINUS SELECT data1, data2, data3 FROM data.QC_CURRENT_DATA_VIEW});
$sth->execute;
open(OUTPUT,">$dir/QC_CLOSED_DATA.csv") || die "can not open file";
while(my @row = $sth->fetchrow_array) {
print OUTPUT join ("\t", @row), "\n";
}
$dbh->disconnect;
Shell Script :
echo "Reports Generated Sucessfully" | mailx -s "Reports Generated Sucessfully" -a "$dir/QC_NEW_DATA.csv" -a "$dir/QC_CLOSED_DATA.csv" prakash@gmail.com
Output :
All the columns are combined into one fields.
T10187693MCCOLLS M DAILY
T1019949SPAR BLAKEMORE
Expected Output in CSV:
Data1 Data2 Data3
T101 87693 "MCCOLLS M DAILY"
T101 99949 "SPAR BLAKEMORE"
|
|
|
Re: export the query into csv file [message #687332 is a reply to message #687331] |
Thu, 23 February 2023 08:20 |
|
Michel Cadot
Messages: 68728 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
Problem is not in Perl:
E:\Temp\Listing>type t.pl
use DBI;
my $dbh = DBI->connect ('dbi:Oracle:mikb2', 'michel', 'michel', {PrintError => 1, AutoCommit => 0 });
my $sth = $dbh->prepare (qq{ SELECT deptno, dname, loc FROM dept});
$sth->execute;
open(OUTPUT,">prakash.csv") || die "can not open file";
while(my @row = $sth->fetchrow_array) {
print OUTPUT join ("\t", @row), "\n";
}
$dbh->disconnect;
E:\Temp\Listing>perl t.pl
E:\Temp\Listing>type prakash.csv
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
Are you sure your files do not contain the tabulations?
Post result of:
head -1 $dir/QC_NEW_DATA.csv | od -xa
|
|
|
|
|
|
|
|