Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何将Perl中的2个CSV文件与DBI和DBD::CSV合并?_Sql_Perl_Dbi - Fatal编程技术网

Sql 如何将Perl中的2个CSV文件与DBI和DBD::CSV合并?

Sql 如何将Perl中的2个CSV文件与DBI和DBD::CSV合并?,sql,perl,dbi,Sql,Perl,Dbi,我需要帮助为perl DBD::CSV包编写SQL语句。我想合并2个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","Zone

我需要帮助为perl DBD::CSV包编写SQL语句。我想合并2个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"
t合并文件应如下所示:

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”语句是错误的。你能帮我找出这份工作的正确答案吗


谢谢。

我不确定DBD::CSV是如何尊重SQL的,但通常如果您
加入
,您还应该在加入内容上说

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

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"],
]

更新: 如果我使用
使用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.
您发布的内容与实际代码之间是否存在差异?你重新打字了吗?即使没有严格的
,也无法编译。它甚至不能输出
[]
,因为它在
print
语句之前就死了