#!/usr/bin/perl use strict; use warnings; use DBD::mysql; use Config::Simple; $|=1; #flush every time the program my $path = 'conf.ini'; my $checkExist; my %config; package MySQL; sub new { Config::Simple->import_from("".$path."", \%config) or die Config::Simple->error(); #my $encrypted = password41($config{'MySQL.pass'}); # for MySQL 4.1 or my $dbh = DBI->connect("dbi:mysql::".$config{'MySQL.host'}.":".$config{'MySQL.port'}."", "".$config{'MySQL.user'}."", #"".$encrypted."", "".$config{'MySQL.pass'}."", { 'PrintError' => 1, 'RaiseError' => 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 ;" , { "mysql_use_result" => 1 }); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } } # End of else my $class = shift; my $data = { _time => shift, _random_number => shift, }; # Print all the values just for clarification. #print "Time is $data->{_time}\n"; #print "Random Number is $data->{_random_number}\n"; bless $data, $class; $checkExist = $dbh->prepare("INSERT IGNORE INTO `".$config{'MySQL.table'}."` (`UnixTime`, `losses`) VALUES ('".$data->{_time}."','".$data->{_random_number}."')" , { "mysql_use_result" => 1 }); if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } $checkExist->finish(); $dbh->disconnect(); return $data; } # End of mysql sub sub getInputDB { my ( $data ) = @_; return $data->{_time} , $data->{_random_number}; } 1;