2012-07-23 84 views
1

我需要幫助寫入perl DBD :: CSV包的SQL語句。我想合併2個CSV文件如下:如何將Perl中的2個CSV文件與DBI和DBD :: CSV合併?

file1.csv:

VM_Name,VM_Cluster,VM_Zone 
VM1," Cluster4","Zone3" 
VM2," Cluster3","Zone4" 
VM3," Cluster2","Zone1" 
VM4," Cluster1","Zone2" 

file2.csv:

VM_Name,vFiler_IP,vFiler_Zone 
VM1," 10.10.10.10","Zone5" 
VM2," 10.10.10.11","Zone8" 
VM3," 10.10.10.12","Zone8" 
VM4," 10.10.10.13","Zone8" 

裏邊反Mergerd文件應該有如下:

VM_Name,VM_Cluster,VM_Zone,vFiler_IP,vFiler_Zone 
VM1," Cluster4","Zone3"," 10.10.10.10","Zone5" 
VM2," Cluster3","Zone4"," 10.10.10.11","Zone8" 
VM3," Cluster2","Zone1"," 10.10.10.12","Zone8" 
VM4," Cluster1","Zone2"," 10.10.10.13","Zone8" 

這是我的Perl代碼:

#!/usr/bin/perl -w 

use strict; 
use Data::Dump qw/dump/; 
use DBI; 

my $dbh = DBI->connect ("dbi:CSV:", 
         "", "", 
         { 
           f_dir  =>  './Desktop', 
           f_schema =>  undef, 
           f_ext  =>  '.csv/r', 
         } 
        ) or die " Cannot create Database Handle: $DBI::errstr()"; 
$dbh->{RaiseError} =1 ; 

my $table1 = "file1"; 
my $table2 = "file2"; 


my $query = "SELECT $table1.VM_Name, $tabel1.VM_Cluster, $table1.VM_Zone, $table2.vFiler_IP, $table2.vFiler_Zone FROM $table1 join $table2 WHERE $table1.VM_Name = $table2.VM_Name"; 
my $result = $dbh->selectall_arrayref ($query); 
print dump ($result); 

輸出應該是我在「[]」括號expencting的talbe,但我得到的是一個空的「[]」括號如下:

[] 

我猜測的「$查詢「我寫的說法是錯誤的。你能幫忙搞清楚這項工作的正確查詢嗎?

謝謝。

回答

1

。在你的SQL語句中的錯字。改變這一行:

my $query = "SELECT $table1.VM_Name, $tabel1.VM_Cluster, $table1.VM_Zone, $table2.vFiler_IP, $table2.vFiler_Zone FROM $table1 join $table2 WHERE $table1.VM_Name = $table2.VM_Name"; 
             ^--- $table1 

您需要使用:

my $query = "SELECT $table1.VM_Name, $table1.VM_Cluster, $table1.VM_Zone, $table2.vFiler_IP, $table2.vFiler_Zone FROM $table1 join $table2 WHERE $table1.VM_Name = $table2.VM_Name"; 

這樣,輸出對我來說是:

[ 
    ["VM1", " Cluster4", "Zone3", " 10.10.10.10", "Zone5"], 
    ["VM2", " Cluster3", "Zone4", " 10.10.10.11", "Zone8"], 
    ["VM3", " Cluster2", "Zone1", " 10.10.10.12", "Zone8"], 
    ["VM4", " Cluster1", "Zone2", " 10.10.10.13", "Zone8"], 
] 

更新: 如果我運行你的代碼與use strict,因爲你發佈錯字,我得到:

Global symbol "$tabel1" requires explicit package name at test.pl line 3726. 

如果我沒有嚴格的運行它,我收到了一堆錯誤:

Bad table or column name: '.VM_Zone' has chars not alphanumeric or underscore! at C:/Perl/site/lib/SQL/Statement.pm line 88 
DBD::CSV::db selectall_arrayref failed: Bad table or column name: '.VM_Zone' has chars not alphanumeric or underscore! at C:/Perl/site/lib/SQL/Statement.pm line 88 
[for Statement "SELECT file1.VM_Name, file1.VM_Cluster, .VM_Zone, file2.vFiler_IP, file2.vFiler_Zone FROM file1 join file2 WHERE file1.VM_Name = file2.VM_Name"] at test.pl line 3727. 
DBD::CSV::db selectall_arrayref failed: Bad table or column name: '.VM_Zone' has chars not alphanumeric or underscore! at C:/Perl/site/lib/SQL/Statement.pm line 88 
[for Statement "SELECT file1.VM_Name, file1.VM_Cluster, .VM_Zone, file2.vFiler_IP, file2.vFiler_Zone FROM file1 join file2 WHERE file1.VM_Name = file2.VM_Name"] at test.pl line 3727. 

之間是否有您發佈什麼,你的實際代碼什麼不同嗎?你重新輸入了嗎?即使沒有strict,也不會編譯。它甚至不能輸出[],因爲它在print聲明之前死亡。

2

我不知道DBD :: CSV如何尊重SQL,但通常如果你join,你也應該說on加入什麼:

select $table1.VM_Name, 
     $tabel1.VM_Cluster, 
     $table1.VM_Zone, 
     $table2.vFiler_IP, 
     $table2.vFiler_Zone 
    FROM $table1 join $table2 
       ON $table1.VM_Name = $table2.VM_Name