my $databases = $dbh->do("SHOW DATABASES LIKE '".$DATABASE."'")
or die "Error: " .dbh->errstr. "\n";
my $databases = $dbh->prepare("SHOW DATABASES LIKE ? ")
or die "Error: " .dbh->errstr. "\n";
$databases->execute($DATABASE)
or die "Error: " . $databases->errstr. "\n";
print "This is the databases: ".$databases."\n";
####
This is the databases: 1
####
This is the databases: DBI::st=HASH(0xe47058)
####
#!/usr/bin/perl
use strict;
use warnings;
use DBD::mysql;
$|=1; #flush every time the program
my $DATABASE = 'xxxx';
my $USERNAME = 'xxxx';
my $TABLE = 'xxxx';
my $HOST = 'xxxx';
my $PORT = 'xxxx';
my $PASS = 'xxxx';
my $checkExist;
sub mysql {
my $dbh = DBI->connect("dbi:mysql::".$HOST.":".$PORT."",
"".$USERNAME."",
"".$PASS."",
{ 'PrintError' => 1, 'RaiseError' => 1 }
) or die "Could not connect to ". $HOST .": ". $DBI::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$DATABASE."'")
or die "Error: " .dbh->errstr. "\n";
=comment
my $databases = $dbh->prepare("SHOW DATABASES LIKE ? ")
or die "Error: " .dbh->errstr. "\n";
$databases->execute($DATABASE)
or die "Error: " . $databases->errstr. "\n";
=cut
print "This is the databases: ".$databases."\n";
exit();
if ($databases eq 1) {
printf "Database: ". $DATABASE ." exists not creating!\n";
}
else {
printf "Database: ". $DATABASE ." does not exist creating!\n";
$checkExist = $dbh->prepare("CREATE DATABASE IF NOT EXISTS `".$DATABASE."`")
or die "Could not create the: ".$DATABASE." error: ". $dbh->errstr ."\n";
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
} # End of else
$dbh->do("USE ".$DATABASE."")
or die "Error: " .dbh->errstr. "\n";
my $tables = $dbh->do("SHOW TABLES FROM `".$DATABASE."` WHERE Tables_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errstr. "\n";
print "Tables do: ".$tables."\n";
$tables = $dbh->prepare("SHOW TABLES FROM `".$DATABASE."` WHERE Tables_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errstr. "\n";
$tables->execute()
or die "Couldn't execute statement: " . $tables->errstr;
print "Tables prepare: ".$tables."\n";
if ($tables eq 1) {
printf "Table: ".$TABLE." exists not creating!\n";
}
else {
printf "Table: ".$TABLE." does not exist creating!\n";
$checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$TABLE."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`UnixTime` int(11) NOT NULL,
`losses` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;");
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
} # End of else
my $range = 50;
my $minimum = 100;
my $random_number = int(rand($range)) + $minimum;
my $time = time();
my $losses = $time . ' ' . $random_number;
$checkExist = $dbh->prepare("INSERT IGNORE INTO `".$TABLE."` (`UnixTime`, `losses`) VALUES ('".$time."','".$random_number."') ");
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
$checkExist->finish();
$dbh->disconnect();
return $losses;
} # End of mysql sub
my $output = &mysql();
print "This is the output: ".$output."\n";