thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I know the title it does not make sense, but this is my main question/problem.
The question evolves several parts and a lot of explanation so please bear with me as it will be a long one with a lot of analysis in between.
I have a working code with do commands and prepare. I know that do is exactly the same with prepare and execute together. I have read O'Reilly's CD bookshelfs/5.5. do( ) Versus prepare( ) that there is no difference between them, unless if you are going to apply multiple loops. Where in this case do will apply multiple requests in comparison to prepare which will execute all requests at once.
So I thought it would be a good idea to become more familiar with prepare and start using it more frequently and not only on INSERT,SELECT and UPDATE statements but everywhere. At this point there is my first problem. I am using prepare on the same piece of code that do works fine and although that I do not get any errors I do get a hash value not an integer. Sample of code provided under:
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";
Output of do command:
This is the databases: 1
Output of prepare command
This is the databases: DBI::st=HASH(0xe47058)
Complete working sample code can be found under:
#!/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 `".$DAT +ABASE."`") or die "Could not create the: ".$DATABASE." error: ". $dbh->er +rstr ."\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 Tabl +es_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errstr +. "\n"; print "Tables do: ".$tables."\n"; $tables = $dbh->prepare("SHOW TABLES FROM `".$DATABASE."` WHERE Ta +bles_in_".$DATABASE." LIKE '".$TABLE."'") or die "Error: " .dbh->errs +tr. "\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_INCREM +ENT=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."` (`Uni +xTime`, `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";
At this point I think the syntax is correct because if I only apply do it works just fine. So can someone elaborate me more why I can not get the same result with do and prepare?
Thank you all for your time and effort assisting me.
|
|---|