Thanks Rob and Jeff, this works:

##########################
#!/bin/perl

use warnings;
use strict;
use DBI;

my $dbs = "dbi:ODBC:DRIVER={servername};SERVER={serverip}";
my ($username, $password) = ('username', 'passwd');

my $dbh = DBI->connect($dbs, $username, $password)
             or die "Can't connect to $dbs: $DBI::errstr";

my $sth = $dbh->prepare("select var1, var2 from mytable")
              or die "Can't prepare statement: $DBI::errstr";

$sth->execute();

my $outfile = 'temp.txt';
open OUTFILE, '>', $outfile or die "Unable to open $outfile: $!";

while(my @re = $sth->fetchrow_array) {
   print OUTFILE @re,"\n";
}

close OUTFILE;

$sth->finish();
$dbh->disconnect();
############################

-L

2008/11/8 Rob Dixon <[EMAIL PROTECTED]>:
> Lauri Nikkinen wrote:
>
> Hi Lauri.
>
> The critical part of your program is this.
>
>> my $outfile = '>temp.txt';
>> open(OUTFILE, $outfile) or die "Unable to open $outfile: $!";
>
> I think you're using a Windows platform where a filename like that is invalid.
> (If you are on Unix and have called a file'>temp.txt' then just don't do 
> that!)
>
> It is far better to keep the file name separate from the open mode and, while
> we're at it, use a lexical file handle. Like this
>
>  my $outfile = 'temp.txt';
>  open my $outfh, '>', $outfile or die "Unable to open $outfile: $!";
>
>> while (<OUTFILE>) {
>
> ...but now you are trying to read from a file that you opened for output. The
> while loop will not execute at all because there is never any input, so
> processing continues...
>
>>     chomp;
>>     my ($var1, $var2) = split /,/;
>>     $sth->execute($var1, $var2);
>>     print $outfile;
>>     }
>
> ...here. All you have done is open an output file and close it.
>
>> close OUTFILE;
>>
>> $sth->finish();
>> $dbh->disconnect();
>> ############################
>>
>> This produces the error message: Filehandle OUTFILE opened only for
>> output at myscript.pl line 28.
>
> That is a clue!
>
> Do you have a file that defines what fields to read from the database? You
> should open that file for input (using an open mode of '<') as well as the
> output file you want to write the fields to. Read the field names from the 
> input
> file, extract the data from the database, and print the data to the output 
> file.
>
> I hope this helps,
>
> Rob
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> http://learn.perl.org/
>
>
>

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to