I am trying to delete some records that satisfy a conditional clause, but the delete statement deletes all records instead. Can someone please help?
Here is the snippet of my code:
use strict;
use warnings;
use DBI;
my $dsn = "DBI:mysql:database=netbackup;host=localhost";
my $dbh = DBI->connect($dsn, 'root', 'password’, {'RaiseError' => 1});
# Database Statement Handles are defined below.
my $sth240 = $dbh->prepare("SELECT * FROM tbl_fsinclude WHERE PolicyID=(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?) AND filesystem LIKE BINARY ?");
my $sth250 = $dbh->prepare("INSERT INTO tbl_fsinclude (Filesystem, PolicyID) VALUES (?,(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?))");
my $sth280 = $dbh->prepare("DELETE FROM tbl_fsinclude WHERE PolicyID=(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?) AND Filesystem NOT IN (?)");
************Part of the main subroutine***********************************
# Insert Filesystems to the Database
if (/^Include:\s+\//) {
printf "Filesystem %s\n", $fields[1] if ($fields[1] =~ /\//);
if ($fields[1] =~ /\//) {
$includefiles[$k++] = $fields[1];
$sth240->execute($policyname, $fields[1]);
if ($sth240->rows == 0) {
$sth250->execute($fields[1], $policyname);
}
}
}
# Call subroutine to delete Filesystems no longer in the policy
if (/^\s+Saturday/ && $done == 0) {
&prunefsinclude();
&prunefsexclude();
$#includefiles = -1;
$#excludefiles = -1;
$newrecord = 0;
$sqlclause100 = "";
$done = 1;
}
****************subroutines***************************************************
sub prunefsinclude {
print "Filesystems in this policy are @includefiles\n";
print "array count for includefiles is $#includefiles.\n";
for $index (0 .. $#includefiles) {
$sqlclause280 .= "\'$includefiles[$index]\'";
$sqlclause280 .= ', ' unless $index == $#includefiles;
}
printf "Deleting filesystems not in include list %s\n",$sqlclause280 if ($sqlclause280);
$sth280->execute($policyname,$sqlclause280);
$sqlclause280="";
}
*****************Snippet of the Database***************************************
mysql> desc tbl_fsinclude;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Filesystem | varchar(60) | YES | | NULL | |
| PolicyID | int(11) | NO | MUL | | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from tbl_fsinclude where PolicyID=158;
+------------+----------+
| Filesystem | PolicyID |
+------------+----------+
| /WS | 158 |
| /hwserver | 158 |
+------------+----------+
2 rows in set (0.00 sec)
mysql> insert into tbl_fsinclude values ('/nas',158);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tbl_fsinclude where PolicyID=158;
+------------+----------+
| Filesystem | PolicyID |
+------------+----------+
| /WS | 158 |
| /hwserver | 158 |
| /nas | 158 |
+------------+----------+
Here is the snippet of my code:
use strict;
use warnings;
use DBI;
my $dsn = "DBI:mysql:database=netbackup;host=localhost";
my $dbh = DBI->connect($dsn, 'root', 'password’, {'RaiseError' => 1});
# Database Statement Handles are defined below.
my $sth240 = $dbh->prepare("SELECT * FROM tbl_fsinclude WHERE PolicyID=(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?) AND filesystem LIKE BINARY ?");
my $sth250 = $dbh->prepare("INSERT INTO tbl_fsinclude (Filesystem, PolicyID) VALUES (?,(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?))");
my $sth280 = $dbh->prepare("DELETE FROM tbl_fsinclude WHERE PolicyID=(SELECT PolicyID FROM tbl_policyattribute WHERE PolicyName LIKE BINARY ?) AND Filesystem NOT IN (?)");
************Part of the main subroutine***********************************
# Insert Filesystems to the Database
if (/^Include:\s+\//) {
printf "Filesystem %s\n", $fields[1] if ($fields[1] =~ /\//);
if ($fields[1] =~ /\//) {
$includefiles[$k++] = $fields[1];
$sth240->execute($policyname, $fields[1]);
if ($sth240->rows == 0) {
$sth250->execute($fields[1], $policyname);
}
}
}
# Call subroutine to delete Filesystems no longer in the policy
if (/^\s+Saturday/ && $done == 0) {
&prunefsinclude();
&prunefsexclude();
$#includefiles = -1;
$#excludefiles = -1;
$newrecord = 0;
$sqlclause100 = "";
$done = 1;
}
****************subroutines***************************************************
sub prunefsinclude {
print "Filesystems in this policy are @includefiles\n";
print "array count for includefiles is $#includefiles.\n";
for $index (0 .. $#includefiles) {
$sqlclause280 .= "\'$includefiles[$index]\'";
$sqlclause280 .= ', ' unless $index == $#includefiles;
}
printf "Deleting filesystems not in include list %s\n",$sqlclause280 if ($sqlclause280);
$sth280->execute($policyname,$sqlclause280);
$sqlclause280="";
}
*****************Snippet of the Database***************************************
mysql> desc tbl_fsinclude;
+------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| Filesystem | varchar(60) | YES | | NULL | |
| PolicyID | int(11) | NO | MUL | | |
+------------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> select * from tbl_fsinclude where PolicyID=158;
+------------+----------+
| Filesystem | PolicyID |
+------------+----------+
| /WS | 158 |
| /hwserver | 158 |
+------------+----------+
2 rows in set (0.00 sec)
mysql> insert into tbl_fsinclude values ('/nas',158);
Query OK, 1 row affected (0.00 sec)
mysql> select * from tbl_fsinclude where PolicyID=158;
+------------+----------+
| Filesystem | PolicyID |
+------------+----------+
| /WS | 158 |
| /hwserver | 158 |
| /nas | 158 |
+------------+----------+