Monday, 25 June 2007

Perl: export table to flat file

Below is the script to extract a table from the mysql database to a flat file.

You need to change the DB_NAME, USERNAME and PASSWORD.

##### Start Script #############
#!/usr/bin/perl -w

use strict;
use DBI;
use DBD::mysql;


my ($dbh, $sql, $sth, $re, $header, $table);

if ($#ARGV ne 0)
{
print "Usage: $0 < TABLE_NAME>\n";
exit;
}

$table=$ARGV[0];

$dbh=DBI->connect('dbi:mysql:DB_NAME','USERNAME','PASSWORD',{AutoCommit => 0, RaiseError => 1}) or die "Unable to connect to DB_NAME: $dbh->err\n";

$sql=<<_KAL_;

select * from $table
_KAL_

$sth = $dbh->prepare($sql) or die "Unable to prepare $sql\n";

$sth->execute();

open(KAL, ">$table.dat") or die "Unable to open $table.dat for writing\n";
$header=$sth->{NAME};
print KAL join('|', @$header), "|\n";
while($re=$sth->fetchrow_arrayref())
{
print KAL join('|', @$re), "|\n";
}
close(KAL);

$sth->finish;

$dbh->disconnect;


##### End Script #############

How to run
copy the contents from start script to end script and place in a file named extract.pl

$ chmod a+x extract.pl
$ ./extract.pl emp
$ more emp.dat

3 comments:

Mirza Sahib said...

salaam khaliq, thanks for the quick script

Anonymous said...

Hi Khaliq, I am actually trying to scan data from an excel sheet so that I can use them in a perl script. Could you please help me on how I can proceed.
My email contact is yogeshbeeharry@yahoo.com

Christopher said...

Great script Khalil. I have similar problem on how to flatten a mysql table, but the way you have it, it's basically extracting the same table from the database which could also be done using sql commands. I need some help on flattening a mysql table for my thesis, but to have data with identical records to be re-ordered in the same columns or rows so that way we eliminate repeated records. Thanks!