esossai
IS-IT--Management
- Jul 16, 2004
- 6
I have a Mysql database where i want to delete some records using this script. I have two diferent scrits as follows: First one list all records where the the "lastdate" is less/equal the date received from command line into $ARG.
The script is the following:
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbuser = "sossai";
my $dbpass = "iassos";
my $dbhost = "localhost";
my $dbname = "ps_stats";
# Dont forget to set the playerid of the player you want to delete
# my $playerid = "10";
my $lastdate = $ARGV[0];
my $debug = 0;
# Connect to the database
print "-- Connecting to MySQL database '$dbname' on '$dbhost' as user '$dbuser' ... " if $debug;
my $db_conn = DBI->connect(
"DBI:mysql:$dbname:$dbhost",
$dbuser,
$dbpass
) or die ("Can't connect to MySQL database '$dbname' on '$dbhost'\n" . "$DBI::errstr\n");
print "connected OK\n\n" if $debug;
my $result = $db_conn->prepare("
SELECT
plrid
FROM
pstats_plr
WHERE
plrlastdate <= ?
ORDER BY
plrid
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($lastdate)
or error("Unable to execute query.\n$DBI::errstr\n");
while (my @rec = $result->fetchrow_array())
{
print "Player Id \" $rec[0] \"\n";
}
$result->finish;
print "-- Disconnecting from database ... " if $debug;
$db_conn->disconnect;
print "disconnected OK\n" if $debug;
exit;
As you can see after the select i have a looping where i print the all read records ids, but what i really want is to send these ids as a parameter to the second script below in order to delete them from the database.
Second script:
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbuser = "sossai";
my $dbpass = "iassos";
my $dbhost = "localhost";
my $dbname = "ps_stats";
# Dont forget to set the playerid of the player you want to delete
my $playerid = "10";
my $debug = 0;
my $table;
my @playerId_tables = (
"pstats_plr",
"pstats_plrids",
"pstats_plrmaps",
);
# Connect to the database
print "-- Connecting to MySQL database '$dbname' on '$dbhost' as user '$dbuser' ... " if $debug;
my $db_conn = DBI->connect(
"DBI:mysql:$dbname:$dbhost",
$dbuser,
$dbpass
) or die ("Can't connect to MySQL database '$dbname' on '$dbhost'\n" . "$DBI::errstr\n");
print "connected OK\n\n" if $debug;
my $result = $db_conn->prepare("
SELECT
Name
FROM
pstats_plrids
WHERE
plrId = ?
LIMIT 1
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($playerid)
or error("Unable to execute query.\n$DBI::errstr\n");
my ($name) = $result->fetchrow_array();
print "Deleting player \"" . $name . "\" ......\n";
foreach $table (@playerId_tables)
{
$result = $db_conn->prepare("
DELETE FROM
$table
WHERE
plrId = ?
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($playerid)
or error("Unable to execute query.\n$DBI::errstr\n");
}
$result->finish;
print "-- Disconnecting from database ... " if $debug;
$db_conn->disconnect;
print "disconnected OK\n" if $debug;
exit;
OK then. The first screen select all records i want to be deleted and the second script actualy delete then (but has the id to be deleted fixex) and i want to receive the id from the first script.
How can i change the first script to call the second one as a subroutine receiving the id as parameter to delete de records?
I appreciate any help.
thanks
esossai
The script is the following:
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbuser = "sossai";
my $dbpass = "iassos";
my $dbhost = "localhost";
my $dbname = "ps_stats";
# Dont forget to set the playerid of the player you want to delete
# my $playerid = "10";
my $lastdate = $ARGV[0];
my $debug = 0;
# Connect to the database
print "-- Connecting to MySQL database '$dbname' on '$dbhost' as user '$dbuser' ... " if $debug;
my $db_conn = DBI->connect(
"DBI:mysql:$dbname:$dbhost",
$dbuser,
$dbpass
) or die ("Can't connect to MySQL database '$dbname' on '$dbhost'\n" . "$DBI::errstr\n");
print "connected OK\n\n" if $debug;
my $result = $db_conn->prepare("
SELECT
plrid
FROM
pstats_plr
WHERE
plrlastdate <= ?
ORDER BY
plrid
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($lastdate)
or error("Unable to execute query.\n$DBI::errstr\n");
while (my @rec = $result->fetchrow_array())
{
print "Player Id \" $rec[0] \"\n";
}
$result->finish;
print "-- Disconnecting from database ... " if $debug;
$db_conn->disconnect;
print "disconnected OK\n" if $debug;
exit;
As you can see after the select i have a looping where i print the all read records ids, but what i really want is to send these ids as a parameter to the second script below in order to delete them from the database.
Second script:
#!/usr/bin/perl -w
use strict;
use DBI;
my $dbuser = "sossai";
my $dbpass = "iassos";
my $dbhost = "localhost";
my $dbname = "ps_stats";
# Dont forget to set the playerid of the player you want to delete
my $playerid = "10";
my $debug = 0;
my $table;
my @playerId_tables = (
"pstats_plr",
"pstats_plrids",
"pstats_plrmaps",
);
# Connect to the database
print "-- Connecting to MySQL database '$dbname' on '$dbhost' as user '$dbuser' ... " if $debug;
my $db_conn = DBI->connect(
"DBI:mysql:$dbname:$dbhost",
$dbuser,
$dbpass
) or die ("Can't connect to MySQL database '$dbname' on '$dbhost'\n" . "$DBI::errstr\n");
print "connected OK\n\n" if $debug;
my $result = $db_conn->prepare("
SELECT
Name
FROM
pstats_plrids
WHERE
plrId = ?
LIMIT 1
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($playerid)
or error("Unable to execute query.\n$DBI::errstr\n");
my ($name) = $result->fetchrow_array();
print "Deleting player \"" . $name . "\" ......\n";
foreach $table (@playerId_tables)
{
$result = $db_conn->prepare("
DELETE FROM
$table
WHERE
plrId = ?
") or error("Unable to prepare query.\n$DBI::errstr\n");
$result->execute($playerid)
or error("Unable to execute query.\n$DBI::errstr\n");
}
$result->finish;
print "-- Disconnecting from database ... " if $debug;
$db_conn->disconnect;
print "disconnected OK\n" if $debug;
exit;
OK then. The first screen select all records i want to be deleted and the second script actualy delete then (but has the id to be deleted fixex) and i want to receive the id from the first script.
How can i change the first script to call the second one as a subroutine receiving the id as parameter to delete de records?
I appreciate any help.
thanks
esossai