I need help in writting the SQL statment for the perl DBD::CSV package. I want to merge 2 CSV files as below:
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"
Tthe Mergerd file should be like below:
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"
Here is my perl Code:
#!/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);
The output should have been the talbe I am expencting in "[]" brackets, but all I get is an empty "[]" bracket as below:
[]
I am guessing that the "$query" statement that I have written is wrong. Could you help in figuring out the correct query for this job.
Thanks.
I am not sure how DBD::CSV respects SQL, but normally if you join
, you should also say on
what to join:
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
There is a typo in your SQL statement. Change this line:
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
You need to use:
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";
With this, the output for me is:
[
["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"],
]
Update : If I run your code with use strict
as you posted it with the typo, I get:
Global symbol "$tabel1" requires explicit package name at test.pl line 3726.
And if I run it without strict, I get a bunch of errors:
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.
Are there any differences between what you have posted and your actual code? Did you retype it? Even without strict
, this does not compile. It cannot even output []
because it dies before the print
statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.