#!/usr/bin/perl -w
# dump.pl
# simple SQL table dump
use strict;
use DBI;
my ($db, $table) = @ARGV;
$table or die "syntax: dump database table\n";
my $driver = 'Your DBD driver name here';
my $dbh = DBI->connect("dbi:$driver:$db",
'myusername',
'mypassword',
{RaiseError=>1})
or die "Can't open: $DBI::errstr\n";
my $sth= $dbh->prepare(qq{select * from $table});
$sth->execute();
my $fields = join "," , @{$sth->{NAME}};
my $insert_statement = qq{INSERT INTO $table ($fields)\n VALUES (};
while (my $row = $sth->fetchrow_arrayref()) {
print $insert_statement,
join( ",", map( {$dbh->quote($_)} @$row)),
");\n";
}
$dbh->disconnect;
__END__
sample output using this "department" table
+-----------+-------------+------------+
| dept_code | dept | sort_order |
+-----------+-------------+------------+
| 1 | Personnel | 5 |
| 2 | Training | 2 |
| 3 | Research | 4 |
| 4 | Sales | 1 |
| 5 | Development | 3 |
+-----------+-------------+------------+
INSERT INTO department (dept_code,dept,sort_order)
VALUES ('1','Personnel','5');
INSERT INTO department (dept_code,dept,sort_order)
VALUES ('2','Training','2');
INSERT INTO department (dept_code,dept,sort_order)
VALUES ('3','Research','4');
INSERT INTO department (dept_code,dept,sort_order)
VALUES ('4','Sales','1');
INSERT INTO department (dept_code,dept,sort_order)
VALUES ('5','Development','3');
####
while (my $row = $sth->fetchrow_arrayref()) {
print join( ",", map( {"\"$_\""} @$row)), "\n";
}
####
print join("\t", @$row),"\n";
####
_ _ _ _
(_|| | |(_|><
_|