Skip to content

Instantly share code, notes, and snippets.

@smeghead
Created July 20, 2011 06:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeghead/1094454 to your computer and use it in GitHub Desktop.
Save smeghead/1094454 to your computer and use it in GitHub Desktop.
dump mysql tables column definitions.
#!/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