Perl:在插入期间忽略csv文件中的列

时间:2014-12-26 21:30:23

标签: oracle perl

我正在将CSV文件插入TAB,

在插入过程中,有什么方法可以忽略CSV文件中的第2和第3列吗?

我尝试将$ row []值更改为$ row [0],$ row [3],$ row [4],$ row [5],$ row [6],但这不起作用。尝试了更多的东西,并在SO内部搜索,但找不到我正在寻找的东西。

有人可以建议或指点我链接吗?

use warnings;    
use strict;
use Text::CSV;
use DBD::Oracle;

my $exitStatus = 0;

dbConnect();
&insertRecords
#----------------
sub insertRecords {
 my $csv;
 my $fileToInsert = shift;
 my $row;
 my $SQL1;
 my $sth;
 my $rc; 

 open my $fh, "<", $fileToInsert or die "$filetoInsert: $!"

 $SQL1 = "Insert into TAB1 (sample_date, server, first, n1, n2)
         values (?,?,?,?,?)";
 $sth = prepare($SQL1)

while ($row = $csv->getline ($fh)) {
    $sth -> execute($row[0], $row[1], $row[2], $row[3], $row[4])
}


CSV File:
sample_date,
date_x1
x2
server
first
n1
n2

1 个答案:

答案 0 :(得分:0)

您需要先读取整个文件,然后运行查询。

现在,查询在每一行上运行。但是你每个条目都有一行 - 而不是你写的每列 - 所以你可能不需要CSV模块?无论如何,那里有几个错误。我已经简化了一点。

尝试这样的事情:

use warnings;    
use strict;
use DBD::Oracle;

my $exitStatus = 0;

dbConnect();
my $file = shift;
&insertRecords($file);

#----------------

sub insertRecords {
  my $fileToInsert = shift;
  my $fh;

  open $fh, "<", $fileToInsert or die "$fileToInsert: $!";

  my $SQL1 = "Insert into TAB1 (sample_date, server, first, n1, n2) values (?,?,?,?,?)";
  my $sth = prepare($SQL1);

  my @row = $fh->getlines;
  print ($row[0], $row[3], $row[4], $row[5], $row[6]); # <-- always good for testing!

  $sth -> execute($row[0], $row[3], $row[4], $row[5], $row[6]);

}