my $dbh = DBI->connect(...);
some_code_that_forks(); # Perhaps without your knowledge
# Child process dies, destroying the inherited dbh
$dbh->do(...); # Breaks because parent $dbh is now broken
####
[MySQL]
user=username
pass=password
host=hostname
port=3306
db=database_name
table=table_name
####
#!/usr/bin/perl
use DBI;
use strict;
use warnings;
use Data::Dumper;
use Config::Simple;
#package LSPDB;
$|=1; #flush every time the program
my $path = 'conf.ini';
my %config = ();
my $checkExist = "";
sub mysql {
Config::Simple->import_from("".$path."", \%config)
or die Config::Simple->error();
my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$config{'MySQL.port'}."",
"".$config{'MySQL.user'}."",
"".$config{'MySQL.pass'}."",
{ 'PrintError' => 1, 'RaiseError' => 1 , 'AutoInactiveDestroy' => 1 }
) or die "Could not connect to ". $config{'MySQL.host'} .": ". $DBI::errstr ."\n";
my $databases = $dbh->do("SHOW DATABASES LIKE '".$config{'MySQL.db'}."'")
or die "Error: " .dbh->errstr. "\n";
if ($databases eq 1) {
printf "Database: ". $config{'MySQL.db'} ." exists not creating: ". $config{'MySQL.db'} ."\n";
}
else {
printf "Database: ". $config{'MySQL.db'} ." does not exist creating: ". $config{'MySQL.db'} ."\n";
$checkExist = $dbh->do("CREATE DATABASE IF NOT EXISTS `".$config{'MySQL.db'}."`")
or die "Could not create the: ".$config{'MySQL.db'}." error: ". $dbh->errstr ."\n";
} # End of else
$dbh->do("USE ".$config{'MySQL.db'}."")
or die "Error: " .dbh->errstr. "\n";
my $tables = $dbh->do("SHOW TABLES FROM `".$config{'MySQL.db'}."` WHERE Tables_in_".$config{'MySQL.db'}." LIKE '".$config{'MySQL.table'}."'") or die "Error: " .dbh->errstr. "\n";
if ($tables eq 1) {
printf "Table: ".$config{'MySQL.table'}." exists not creating: ".$config{'MySQL.table'}."\n";
}
else {
printf "Table: ".$config{'MySQL.table'}." does not exist creating: ".$config{'MySQL.table'}."\n";
$checkExist = $dbh->prepare("CREATE TABLE IF NOT EXISTS `".$config{'MySQL.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 `".$config{'MySQL.table'}."` (`UnixTime`, `losses`) VALUES ('".$time."','".$random_number."') ");
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
my $statement = "SELECT * FROM `".$config{'MySQL.table'}."` WHERE 1";
my $hash_ref = $dbh->selectall_hashref($statement, 'id');
=arrayref
# get array of id and name pairs:
my $ary_ref = $dbh->selectcol_arrayref($statement, { Columns=>[1,3] });
my %hash = @$ary_ref; # build hash from key-value pairs so $hash{$id} => name
print Dumper(\%hash);
=cut
=count rows
$checkExist = $dbh->prepare("SELECT COUNT(*) FROM `".$config{'MySQL.table'}."`");
if (!$checkExist->execute()) {
die "Error: ". $checkExist->errstr ."\n";
}
my ($crows) = $dbh->selectrow_array($checkExist);
print $crows . "\n";
=cut
$checkExist->finish();
$dbh->disconnect()
or warn "Error disconnecting: $DBI::errstr\n";
return $hash_ref;
} # End of mysql sub
my $output_ref = mysql();
print Dumper($output_ref);
__END__
First Round:
Database: Speed does not exist creating: Speed
Table: Data does not exist creating: Data
$VAR1 = {
'1' => {
'UnixTime' => '1413048302',
'losses' => '102',
'id' => '1'
}
};
Second Round:
$VAR1 = {
'1' => {
'UnixTime' => '1413048302',
'losses' => '102',
'id' => '1'
},
'2' => {
'losses' => '135',
'UnixTime' => '1413048306',
'id' => '2'
}
};