Adding the commas is really a PERL question.
To insert commas use "join" on the array.
Something like: $joinedline=join(',',@row);
This will take all fields and combine them, seperated with a comma.  You will have
an issue if your text has commas in it so you have to qoute the text fields or if you
can control the delimiter on the other side you can use "join" with your own seperator.

Andy

-----Original Message-----
From: Michael A. Chase [mailto:[EMAIL PROTECTED]]
Sent: Monday, July 16, 2001 9:14 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: (Fwd) How to write records from table to csv file?


The only problem I see is that you are closing the output file as soon as
you have fetched and written the first line.

You really need to include 'use strict;' near the top of your program and
'-w' on the '#!' line.  They would help spot problems like that sooner.

Including $! in the file open failure message would also be useful if it
ever does fail.
--
Mac :})
** I normally forward private questions to the appropriate mail list. **
Give a hobbit a fish and he eats fish for a day.
Give a hobbit a ring and he eats fish for an age.
----- Original Message -----
From: "Tim Bunce" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>
Sent: Monday, July 16, 2001 08:39
Subject: (Fwd) How to write records from table to csv file?


> ----- Forwarded message from Vinod Patel <[EMAIL PROTECTED]> -----
>
> From: Vinod Patel <[EMAIL PROTECTED]>
> To: "'Tim Bunce'" <[EMAIL PROTECTED]>
> Subject: How to write records from table to csv file?
> Date: Mon, 16 Jul 2001 09:32:50 -0400
> Return-Receipt-To: Vinod Patel <[EMAIL PROTECTED]>
>
> I am trying to write a comma delimited file from table.
> Its writing only first record not all of the records. I also need to place
> comma between fields.
> Could you check my code please? any suggestion.
>
> # Select records from table and save into array.
> sub select_records {
>   my $sth;
>   my ($pass_owner_name, $pass_table_name) = @_;
>   my $rows;
>   my $line;
>   my @rows;
>
>   my $dirlog = $ENV{'ARCHIEVE_FILE_DIR'};
>   my $curr_dt = sprintf
> "%4d%02d%02d",$time->year+1900,($time->mon)+1,$time->md
> y;
>   my $archfile=sprintf "%s/%s.%s_%d.csv%s", $dirlog, $pass_owner_name,
> $pass_ta
> le_name, $curr_dt;
>
>   open(ARCH_FILE, ">$archfile") || process_die ("Can't open $archfile file
> \n")
>
>   $sth=
>     $dbh->prepare( "
>            select *
>              from tablename
>          " );
>
>   if ( !defined $sth ) {
>     process_die("Cannot prepare select from table $DBI::errstr\n");
>   }
>
>   # execute the statement
>   $sth->execute() || process_die( $sth->errstr() );
>
>  while ( (@rows) = $sth->fetchrow_array() ) {
>  print "ROW: @rows\n";
>  print(ARCH_FILE @rows);
> close(ARCH_FILE);
>  }  # while
> }

Reply via email to