MySQLテーブル一覧作成スクリプト

 #!/usr/bin/perl
 
 use strict;
 use Data::Dumper;
 
 # read db name or die
 my $dbname = shift or die "usage: $0 dbname\n";
 
 # set current date
 my @lt = localtime;
 my $date = sprintf "%04d/%02d/%02d %02d:%02d",
     $lt[5]+1900,$lt[4]+1,$lt[3],$lt[2],$lt[1];
 
 # get info of all tables
 my @tables = get_tables();
 
 # make html of tables info
 my $t_html = make_table_html(@tables);
 
 # build html
 my $html;
 {
     local $/;
     $html = <DATA>;
 }
 $html =~ s/<DBNAME>/$dbname/;
 $html =~ s/<DATE>/$date/;
 $html =~ s/<T_HTML>/$t_html/;
 
 # show
 print $html;
 
 exit;
 
 # ----------------------------------------------------------------------
 sub get_tables {
     my @tables = `mysql -N -B -e 'show tables' $dbname`;
     return @tables;
 }
 # ----------------------------------------------------------------------
 sub make_table_html {
     my (@tables) = @_;
     my $html;
     my $cnt;
     foreach my $t(@tables){
         $html .= "<table>\n";
         $html .= "<tr><th colspan=\"2\">$t</th></tr>";
         my @rows = `mysql -N -B -e 'desc $t' $dbname`;
         foreach my $r(@rows){
             my @c = split "\t", $r;
             $html .= sprintf "<tr><td>%s</td><td>%s</td></tr>\n", 
                 $c[0],$c[1];
         }
         $html .= "</table>\n";
         $html .= sprintf "<hr>\n" if ++$cnt % 3 == 0;
     }
     return $html;
 }
 __DATA__
 <html>
 <head>
 <style type="text/css">
 body {
 font-family: 'Verdana',sans-serif;
 background: #eee;
 }
 table {
 width: 30%;
 float: left;
 margin: 0 15px 15px 0;
 border: 1px solid #444;
 background: #fff;
 }
 th, td {
 empty-cells: show;
 }
 th {
 color: #fff;
 background: #444;
 font-size: 80%;
 letter-spacing: 2px;
 }
 td {
 font-size: 80%;
 background: #ddd;
 }
 hr {
 clear: both;
 visibility: hidden;
 }
 </style>
 </head>
 <body>
 <h1><DBNAME></h1>
 <div class="date"><DATE></div>
 <T_HTML>
 </body>
 </html>

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS

Last-modified: 2006-05-16 (火) 05:40:12