Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/perl -w
use strict;
use DBIx::Dump;
use DBI;
# ... connect to your database as normal
my $database='sdeodyssey11';
my $dbh = DBI->connect("DBI:mysql:$database", 'root', '') or die "Couldn't connect to database: " . DBI->errstr;
my $sth = $dbh->prepare("SELECT table_name,table_rows FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$database'");
$sth->execute();
while (my $row = $sth->fetchrow_hashref)
{
my $tablename=$row->{'table_name'};
print "$tablename\n";
my $format='excel';
my $extension="xls";
if ($row->{'table_rows'}>63000)
{
$format='csv';
$extension="csv";
}
my $filename=$tablename.".".$extension;
my $sth2 = $dbh->prepare("select * from $database.".$tablename); # your query here
$sth2->execute();
my $out = DBIx::Dump->new('format' => $format, # excel or csv
'output' => $filename, # file to save as
'sth' => $sth2);
$out->dump( );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.