Created
July 20, 2011 06:31
-
-
Save smeghead/1094454 to your computer and use it in GitHub Desktop.
dump mysql tables column definitions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use utf8; | |
sub options { | |
die "usage: $0 <user> <password> <host> <database>\n" if scalar(@ARGV) != 4; | |
return { | |
user => $ARGV[0], | |
password => $ARGV[1], | |
host => $ARGV[2], | |
database => $ARGV[3], | |
}; | |
} | |
sub mysql_command { | |
my $options = shift; | |
my $command = "mysql -u $options->{user} "; | |
$command .= "-p$options->{password} " if $options->{password}; | |
$command .= "-h $options->{host} " if $options->{host}; | |
$command .= "$options->{database}"; | |
return $command; | |
} | |
my $options = options(); | |
my $command = "echo show tables | " . mysql_command($options); | |
my @tables = split(/\s/, `$command`); | |
for my $t (@tables) { | |
print "=== $t ===\n"; | |
my $command = "echo show columns from $t | " . mysql_command($options); | |
my @columns = split(/\n/, `$command`); | |
for my $line (@columns) { | |
print "$t\t$line\n"; | |
} | |
print "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment