Hello all,
First time using the DBI module here. I have the following code:
Here's what the query looks like in SQLPLUS
What I want is a hash like this:
Juniper => 205.152.47.32
If there is no ip associated with the vendor I want to skip it. If there is no vendor associated with the ip skip that as well. While I am at it I want to use the hash to de-duplicate the ip entries.
I have been trying many iterations of my script to no avail. Here is the output I am getting with my current code:
As you can see when there is no vendor associated with an ip it switches key for value for the rest of the entries.
How that explains it ok.
Thanks,
Nick
If at first you don't succeed, don't try skydiving.
First time using the DBI module here. I have the following code:
Code:
use DBI;
#use DBD::Oracle;
use strict;
#use warnings;
my $orauser = "xxxxx"
my $orapass = "xxxxx"
my $vendor;
my $ip;
my %db_results;
my $data_source = "dbi:Oracle:binvdp01";
my $dbh = DBI->connect($data_source, $orauser, $orapass)
or die "Can't connect to $data_source: $DBI::errstr";
my $sth = $dbh->prepare( q{select T1.SVALUE, T2.SVALUE from twtpropvalues T1, twtpropvalues T2 where
T1.PROPID=(select ID from twtproperties where WID='00000000000000000000000000000001')
and T2.PROPID=(select ID from twtproperties where
WID='72B1577C795AB24888F4462B18D8F42D') and T1.INSID=T2.INSID}
) or die "Can't prepare statement: $DBI::errstr";
my $rc = $sth->execute
or die "Can't execute statement: $DBI::errstr";
print "Query will return $sth->{NUM_OF_FIELDS} fields.\n\n";
print "Field names: @{ $sth->{NAME} }\n";
while (($ip, $vendor) = $sth->fetchrow_array) {
next unless ($ip and $vendor);
$db_results{$ip} = $vendor;
}
# check for problems which may have terminated the fetch early
die $sth->errstr if $sth->err;
$dbh->disconnect;
while ( my($k,$v) = each %db_results ) {
$db_results{$v} = $k;
}
while ( my($k,$v) = each %db_results ) {
print "$k => $v\n";
}
Here's what the query looks like in SQLPLUS
Code:
SQL> select T1.SVALUE, T2.SVALUE from twtpropvalues T1, twtpropvalues T2 where T1.PROPID=(select ID
from twtproperties where WID='00000000000000000000000000000001') and T2.PROPID=(select ID from twtpr
operties where WID='72B1577C795AB24888F4462B18D8F42D') and T1.INSID=T2.INSID;
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.32
Juniper
205.152.47.34
Juniper
205.152.47.40
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.39
Cisco
205.152.47.34
Juniper
205.152.47.36
Juniper
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.40
Cisco
205.152.47.38
Juniper
205.152.47.37
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.37
Cisco
205.152.47.37
Cisco
205.152.47.37
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.37
Cisco
205.152.47.39
Cisco
205.152.47.41
Juniper
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.32
Juniper
205.152.47.35
Juniper
205.152.204.240
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.204.240
Cisco
205.152.47.39
Cisco
205.152.47.38
Juniper
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.41
Juniper
205.152.207.210
205.152.47.35
Juniper
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.36
Juniper
205.152.47.40
Cisco
205.152.47.34
Juniper
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.33
Juniper
205.152.47.32
Juniper
205.152.47.37
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.204.210
Cisco
172.29.98.3
Cisco
192.168.42.2
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
192.168.40.2
Cisco
192.168.43.2
Cisco
205.152.204.210
Cisco
SVALUE
--------------------------------------------------------------------------------
SVALUE
--------------------------------------------------------------------------------
205.152.47.32
Juniper
205.152.204.210
Cisco
38 rows selected.
What I want is a hash like this:
Juniper => 205.152.47.32
If there is no ip associated with the vendor I want to skip it. If there is no vendor associated with the ip skip that as well. While I am at it I want to use the hash to de-duplicate the ip entries.
I have been trying many iterations of my script to no avail. Here is the output I am getting with my current code:
Code:
D:\Stage>db_conn_test.pl
Query will return 2 fields.
Field names: SVALUE SVALUE
205.152.204.240 => Cisco
205.152.47.40 => Cisco
205.152.47.32 => Juniper
205.152.47.33 => Juniper
205.152.47.41 => Juniper
205.152.47.34 => Juniper
192.168.40.2 => Cisco
205.152.47.35 => Juniper
205.152.47.36 => Juniper
205.152.47.37 => Cisco
205.152.47.38 => Juniper
205.152.47.39 => Cisco
192.168.42.2 => Cisco
Juniper => 205.152.47.38
Cisco => 205.152.204.210
192.168.43.2 => Cisco
172.29.98.3 => Cisco
205.152.204.210 => Cisco
As you can see when there is no vendor associated with an ip it switches key for value for the rest of the entries.
How that explains it ok.
Thanks,
Nick
If at first you don't succeed, don't try skydiving.