我需要帮助来编写 perl DBD::CSV 包的 SQL 语句。我想合并 2 个 CSV 文件,如下所示:
文件 1.csv:
VM_Name,VM_Cluster,VM_Zone
VM1," Cluster4","Zone3"
VM2," Cluster3","Zone4"
VM3," Cluster2","Zone1"
VM4," Cluster1","Zone2"
文件 2.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"
合并文件应如下所示:
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);
输出应该是我在“[]”括号中使用的 talbe,但我得到的只是一个空的“[]”括号,如下所示:
[]
我猜我写的“$query”语句是错误的。你能帮忙找出这个工作的正确查询吗?
谢谢。