TXT TO SQL 変換10
戻る
# SQLファイルを生成
# make_tables.pl
use strict;
my (%t,$n,@fld,@list);
# 設定データファイルを読む
open(IN,"../txt/tables.txt") or die "Can't open the file tables.txt.\n";
while() {
@fld = split;
last if (/^END/);
if ( /^TABLE/ ) {
$t{table} = $fld[1];
} elsif ( /^LIST/ ) {
chop;
@fld = split(/,/);
$fld[1] =~ s/\s*//;
$fld[2] =~ s/\s*//;
$fld[3] =~ s/\s*//;
push(@{ $t{name} },$fld[1]);
push(@{ $t{type} },$fld[2]);
push(@{ $t{example} },$fld[3]);
}
}
close(IN);
$t{sqlfile} = $t{table} . '.sql';
open(OUT,">../sql/$t{sqlfile}");
print OUT 'DROP TABLE IF EXISTS ';
print OUT $t{table},';',"\n";
print OUT 'CREATE TABLE ';
print OUT $t{table},"\n";
print OUT '(',"\n";
print OUT ' id INT AUTO_INCREMENT,',"\n";
for $n ( 1 .. $#{ $t{name} } ) {
$t{name1} = $t{name}[$n];
$t{type1} = $t{type}[$n];
printf OUT ("\t%-16s ",$t{name1});
print OUT $t{type1},",\n";
}
print OUT ' PRIMARY KEY (id)',"\n";
print OUT ');',"\n\n";
print OUT 'INSERT INTO ';
print OUT $t{table};
$t{name1} = ' (';
for $n ( 1 .. $#{ $t{name} } ) {
if ( $#{ $t{name} } == $n ) {
$t{name1} = $t{name1} . $t{name}[$n];
} else {
$t{name1} = $t{name1} . $t{name}[$n] . ',';
}
}
print OUT $t{name1},') VALUES(';
$t{example1} = '"';
for $n ( 1 .. $#{ $t{example} } ) {
if ( $#{ $t{example} } == $n ) {
$t{example1} = $t{example1} . $t{example}[$n] . '"';
} else {
$t{example1} = $t{example1} . $t{example}[$n] . '","';
}
}
print OUT $t{example1},');';
print OUT "\n";
close(OUT);
print "The output file is ../sql/$t{sqlfile}.\n";
exit;
__END__;
--------------------------------------------------------------------------------
Filename tables.txt
C---+----1----+----2----+----3----+----4----+----5----+----6----+----7
TABLE enq2
C name type example
LIST, id, INT, 1
LIST, time, date, 2007-12-02
LIST, our_refid, INT NOT NULL, 32
LIST, owners_code, char(50) NOT NULL, 33
LIST, hull_noid, INT NOT NULL, 34
LIST, main_name1id, INT NOT NULL, 35
LIST, main_type1id, INT NOT NULL, 36
LIST, main_maker1id, INT NOT NULL, 38
LIST, main_dwg1id, INT NOT NULL, 29
LIST, parts1id, char(200) NOT NULL, 23=34=234
LIST, parts_NUid, char(200) NOT NULL, 1=1=1
LIST, memo, char(200) NOT NULL, A test memo
END
TABLE enq1
C name type example
LIST, id, INT, 1
LIST, time, date, 2007-11-02
LIST, our_refid, INT NOT NULL, 32
LIST, owners_code, char(50) NOT NULL, 33
LIST, hull_noid, INT NOT NULL, 34
LIST, main_name1id, INT NOT NULL, 35
LIST, main_type1id, INT NOT NULL, 36
LIST, main_maker1id, INT NOT NULL, 38
LIST, main_dwg1id, INT NOT NULL, 29
LIST, parts1id, char(200) NOT NULL, 23=34=234
LIST, parts_NUid, char(200) NOT NULL, 1=1=1
LIST, memo, char(200) NOT NULL, A test memo
戻る