0

I need a little help in redirecting the output of a SQL query to a file. My code looks like this:

my $sth = $dbh->prepare(
    "select count(parameter2),
    parameter2 as file_type
    from KCRT_TABLE_ENTRIES where request_id = $mycrnum
    group by parameter2"
) or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";

$sth->execute > $mydir\\file_detail.txt
    or die "Can't execute SQL statement: ", $sth->errstr(), "\n";
5
  • I want the output of sql query to be saved on $mydir\\file_detail.txt. Commented Aug 20, 2015 at 21:21
  • If I remove the redirect option(> $mydir\\file_detail.txt) from the query, the script runs fine. I'm not able to redirect the value of $sth in a file. Commented Aug 20, 2015 at 21:23
  • 7
    Perl is not a shell; it doesn't have redirection operators. Commented Aug 20, 2015 at 21:39
  • Please let me know how to redirect the output then. I tried multiple options, and it doesn't seem working. Commented Aug 20, 2015 at 21:41
  • 1
    Did you look at the DBI docs? It kind of feels like you didn't, because there are actually complete examples in the docs that show how to select and fetch data. Commented Aug 21, 2015 at 0:13

3 Answers 3

5

I've had to invent a lot of code as you don't show much of your program, but the program below gives you the rough idea

Once you've called execute you have to call one of the fetch methods to retrieve the data in whatever form is most useful to you. Here I've just asked for a reference to an array containing each row's data

Then it's simply a matter of opening the required file for output and printing the rows of data to it

I've removed the status checks on each DBI call and replaced it with the RaiseError flag which does the same thing automatically. I've also replaced the parameter $mycrnum in the SQL statement with a placeholder and passed its value to execute. That way DBI looks after any necessary quoting etc.

use strict;
use warnings;

use DBI;

my ($dsn, $user, $pass);

my ($mycrnum, $mydir);

my $dbh = DBI->connect($dsn, $user, $pass);
@{$dbh}{qw/ PrintError RaiseError /} = (0, 1);

my $sth = $dbh->prepare(
    "SELECT COUNT(parameter2),
    parameter2 AS file_type
    FROM kcrt_table_entries
    WHERE request_id = ?
    GROUP BY parameter2"
);

$sth->execute($mycrnum);

open my $fh, '>', "$mydir/file_detail.txt" or die $!;
select $fh;

while ( my $row = $sth->fetchrow_arrayref ) {
    printf "%5d %s\n", @$row;
}
Sign up to request clarification or add additional context in comments.

Comments

2

After the execute, open the output file:

open my $of, ">", "$mydir\\file_detail.txt";

Then read each line (or row) in the results:

while ( @row = $sth->fetchrow_array ) {

Printing the output to the opened file handle:

    print $of "@row\n";  # NO COMMA AFTER $of!

Close the while() loop:

}

Finally, close your opened file handle:

close $of;

Now your done.

3 Comments

I tried the option, but somehow the file did not get created. There were no error though.
If you do an or die $! after the open, you'll definitely see an error if something went wrong.
If I had to guess, the problem might be that you didn't $sth->execute. I think Jim's code presupposes your $sth has been properly set up prior to this point.
1

Something like this, perhaps?

my $sth = $dbh->prepare(q{
    select count(parameter2),
    parameter2 as file_type
    from KCRT_TABLE_ENTRIES where request_id = ?
    group by parameter2
}) or die "Can't prepare SQL statement: ", $dbh->errstr(), "\n";

$sth->execute($mycrnum);

open my $OUT, '>', "$mydir/file_detail.txt" or die;

while (my @row = $sth->fetchrow_array) {
  print $OUT @row, "\n";   # or whatever...
}

close $OUT;

$sth->finish;

This is a little bit of overkill, since you are only reading a single value, but it at least demonstrates a boilerplate for getting it done for future queries.

If you ever have a guaranteed single row, you can do something like this:

my ($val1, $val1) = $dbh->selectrow_array(q{
    select foo, bar
});

7 Comments

I tried this option. There seems to be some syntax error. I'll check it tomorrow and try to get it work.
The syntax error is the unclosed brace for q{} in the first line.
Matt is right... I didn't close the brace on the q{ }. That's fixed.
I tried in similar way. After giving the die command, its showing error at open my $out(at line 5446). Died at sblmaint.pl line 5446, <> line 3.
The file file_detail.txt need to be created at runtime. The directory $mydir is being created at runtime. Is this why the script is failing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.