600を超えるtxtファイルをMySQLにインポートし、perlと次のコードを使用しようとしていますが、機能していないようです。接続してから切断するという意味では、各ファイルを開いて指示どおりに処理するようには見えないため、インポートテーブルは空のままです。ディレクトリが存在するので、コードに明らかなエラーがない限り、何が間違っているのかわかりません。perlスクリプトを実行してもエラーメッセージは表示されません。各ファイルには、1行に1つのレコードがあり、レコードの終わりを示す\n文字が付いています。私は誰かが助けてくれることを願っています、この小さな問題は当惑していて、それがうまくいかない理由についての明白な理由を私は見ることができません。
use strict;
use warnings;
use DBI;
# Set datasource
my $dsn = "DBI:mysql:host=somehost;database=somedb"
. ";port=someport";
# Connect to database
my $dbh = DBI->connect($dsn,"someuser","somepassword");
print "Status: Connected to MySQL.\n";
my $dir = "t:\\some directory";
opendir (DIR, $dir) or die "Unable to open directory";
my @files = grep /\.txt/, readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
open(FH, '<', "$dir\\$file") or die "Unable to open $file - $!\n";
while (<FH>){
my $data = split (/\n$/);
my $insert = $dbh->prepare('INSERT INTO sometable
(TEXTDATA,SOURCEFILE)
VALUES (?,?)') or die "Prepare failed: " . $dbh->errstr();
$insert->execute($data,$file) or die "Execute failed: " . $dbh->errstr();
}
close(FH);
}
print "Status: Processing of complete.\n";
# Disconnect from the database
$dbh->disconnect ();
print "Status: Disconnected from MySQL.";