TXT TO SQL 変換1
戻る
C:\database\sql>mysql cookbook < owners.txt -u cbuser -p
Enter password: ******
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the ma
nual that corresponds to your MySQL server version for the right syntax to use n
ear '
company==>XXX
address==>201,YYY' at line 1
C:\database\sql>mysql cookbook < owners.sql -u cbuser -p
Enter password: ******
owners.txtではなく,owners.sqlです!
---------------------------------------------------------------
# txt2sql1.pl
use strict;
use Encode;
my(%t,$n,$n1,@fld);
@{ $t{name} } = qw/company address person telfax email homepage memo/;
# テキストファイルを入力
open(IN,"../sql/owners.txt") or die "Can't open the file owners.txt\n";
while(){
chop;
next if $. == 1;
@fld = split(/==>/);
next if (length($fld[0]) < 3 );
push(@{ $t{list}{$fld[0]} },$fld[1]);
}
close(IN);
# テーブルを生成
open(OUT,">../sql/owners.sql");
print OUT 'DROP TABLE IF EXISTS owners;',"\n";
print OUT 'CREATE TABLE owners', "\n";
print OUT '(',"\n";
print OUT "\t",'id INT AUTO_INCREMENT,',"\n";
print OUT "\t",'company CHAR(100),',"\n";
#print OUT "\t",'adderss CHAR(100),',"\n"; #Bug!
print OUT "\t",'address CHAR(100),',"\n";
print OUT "\t",'person CHAR(50),',"\n";
print OUT "\t",'telfax CHAR(100),',"\n";
print OUT "\t",'email CHAR(100),',"\n";
print OUT "\t",'homepage CHAR(100),',"\n";
print OUT "\t",'memo CHAR(100),',"\n";
print OUT "\t",'PRIMARY KEY (id)',"\n";
print OUT ');',"\n\n";
# データを挿入
for $n ( 0 .. $#{ $t{list}{company} } ) {
print OUT 'INSERT INTO owners (company,address,person,telfax,email,homepage,memo) VALUES("';
for $n1 ( 0 .. 5 ) {
$t{name1} = $t{name}[$n1];
$t{one} = $t{list}{$t{name1}}[$n];
if (length($t{one}) < 3 ) {
$t{one} = 'XXX';
}
print OUT $t{one},'","';
}
print OUT 'XXX")';
print OUT "\n";
}
__END__;
戻る