programming-examples/perl/Database/Deleting Entries.pl
2019-11-15 12:59:38 +01:00

20 lines
606 B
Perl

use DBI;
my $driver="DBI:mysql";
my $database="sample_db";
my $user="root";
my $host="localhost";
my $dbh = DBI->connect("$driver:database=$database;host=$host;user=$user") or die "Can't connect: " . DBI->errstr;
print "Name: ";
chomp($name=<STDIN>);
my $sth=$dbh->prepare('SELECT count(*) from Employee WHERE name = ?');
$sth->execute($name);
print "Number of rows to be deleted: ", $sth->fetchrow_array(), "\n";
$num=$dbh->do(qq/DELETE from Employee WHERE name = ?/, undef,$name);
print ($num > 1 ?"$num rows deleted.\n":"$num row deleted.\n");
$sth->finish();
$dbh->disconnect();