Actually the program, let's call it mysql.pl
#!/usr/bin/perl -w -- use warnings; use strict; use DBI; my $DSN = "DBI:mysql:database=test;host=localhost"; sub main { my $sql = $ARGV[0]; my $dbh = DBI->connect( $DSN, undef, undef, { RaiseError => 1, AutoCommit => 0 } ) or die "Connect error: " . $DBI::errstr; my $sth = $dbh->prepare( $sql ) or die "Prepare error: " . $dbh->errstr; $sth->execute or die "Execute error: " . $dbh->errstr; print join("\t", @{ $sth->{NAME} } ), "\n"; while ( my $row = $sth->fetch ) { print join("\t", map { defined($_) ? $_ : 'NULL'} @{ $row }), "\n"; } $sth->finish; $dbh->disconnect; } &main; 1; __END__
Performs queries to the test database on localhost, and prints the results to stdout, the fields are separated by a tab, use this:
% ./mysql.pl "select * from table" > output.file
If something is not clear - ask