Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to combine two scripts

Status
Not open for further replies.

esossai

IS-IT--Management
Jul 16, 2004
6
0
0
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
 
Orange on purple, I'm not even going to look at that

use the
Code:
 tags like everyone else

--Paul



It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
[ignore]
Code:
your script here
[/ignore]

as Paul says orange on purple is nasty - I need to highlight the text to be able to read it!


Kind Regards
Duncan
 
Been answered on forums.devshed.com, and there's orange over there too
--Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
OK since i couldn'f find the edit option i reply it back without the colors. Thanks.

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:

Code:
#!/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:

Code:
#!/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
 
Yes they did answer but i did not receive an e-mail. i will check. thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top