私は、テキストファイルの内容をMySQLデータベースにロードしています。私はテーブルを正しくロードしましたが、単にテーブルの見出しである最初の行をスキップすることはできませんでした。MySQLにテキストファイルをロードする - 最初の行をスキップする
ここには、このスレッドのすべてのコメントを考慮して書いたコードがあります。私は、テキストファイルのヘッダーをスキップするファイルを開いたが、それはまだ動作していないようだ後、私はライン
if($. == 1) {
を追加しました。
ご迷惑をおかけしております。
提案がありますか?
#!/usr/bin/perl -w
use DBI;
use strict;
use Data::Dumper;
my $user = shift @ARGV or die $!;
my $password = shift @ARGV or die $!;
my $database = shift @ARGV or die $!;
my $recipient_ewes = shift @ARGV or die $!;
my $dbh = DBI->connect("DBI:mysql:$database:localhost",
$user,
$password,
{RaiseError => 1}
);
open (FILE, "rid.txt") or die $!;
while (<FILE>) {
if($. == 1) {
next;
}
my $GID;
my $RID;
my $number;
my $line = $_;
chomp $line;
my @array = split("\t",$line);
if (scalar(@array)==4){
$RID = $array[0];
$GID = $array[2];
$number = $array[3];
my $sth = $dbh-> prepare (qq{insert into $recipient_ewes (GID, RID, numbertransferred) values ("$GID", "$RID", "$number")});
$sth -> execute();
$sth -> finish();
}
}
$dbh->disconnt();
exit;
close FILE;
入力ファイルは次のとおりです。
Recipient ID Round Group # # of transfers
6507 1 2 4
5101 1 4 4
5007 1 5 3
6535 2 6 4
6510 2 7 4
'next if $。 == 1; '直後' while(...){' –
残念ながらそれはうまくいきませんでした。そのコマンドは何をすべきか? – Elley
それはうまくいきますが、[mcve]がなければ、あなたをさらに助けることは不可能です。 –