【发布时间】:2017-04-21 21:10:02
【问题描述】:
我正在将文本文件的内容加载到 MySQL 数据库中。我已经正确加载了表格,但我无法跳过第一行,它只是表格标题。
这是我编写的代码,考虑了该线程上的所有 cmets。我添加了一行
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 (...) {之后 -
不幸的是,这不起作用,我尝试将其放在多个位置。该命令应该做什么?
-
它确实有效,但没有minimal reproducible example,就无法进一步帮助您。
-
您可以阅读 perl 的特殊变量,例如
$.here。$.包含当前文件句柄的行号。此页面还将告诉您为什么不应该使用$1、$2等。在页面上搜索$<digits>。我同意@MattJacob 的观点,如果您需要更多帮助,您需要编辑您的问题以获得可用的代码块。 -
您似乎正试图将数据插入到名为
$recipient的表中,但您没有声明这样的变量;只有哈希%recipient。