Dear Monks,

Thank you for your time and effort reading and relpying to my question.

I am sure that this question it is not a challenge for most of you but it seems that I can not find an answer to such a simple question.

I want to create a Database, table, and insert some data on MySQL database. The problem comes that I also want to check if all of these elements exist before I send the request to create them.

The only solution that I have found so far is to connect and disconnect from the DB after the completion of one process. This not only makes my code slower but also I am sending multiple request.

I am sure that it can be done with one request from beginning. I tried to find a command to use such as use "db" same as MySQL syntax, but so far not successfully.

The reason that I am connecting and disconnecting is that the script will complain that it "can not find the database" alternatively.

Sample of my code is provided under:

#!/usr/bin/perl use strict; use warnings; use DBD::mysql; $|=1; #flush every time the program my $db = 'xxxx'; my $port = '3306'; # default my $host = 'localhost'; # or external my $pass = 'xxxx'; my $user = 'xxxx'; my $table = 'xxxx'; my $checkExist; sub mysql { my $dbh = DBI->connect("dbi:mysql::".$host.":".$port."", "".$user."", "".$pass."", { 'PrintError' => 1, 'RaiseError' => 1 } ) or die "Could not connect to ".$host.": ". $DBI::errstr ."\n"; my $databases = $dbh->do("SHOW DATABASES LIKE '".$db."'") or die " +Error: " .dbh->errstr. "\n"; if ($databases eq 1) { printf "Database: ".$db." exists not creating: ".$db."\n"; } else { printf "Database: ".$db." does not exist creating: ".$db."\n"; $checkExist = $dbh->prepare("CREATE DATABASE IF NOT EXISTS `".$db. +"`") or die "Could not create the: ".$db." error: ". $dbh->errstr ."\ +n"; if (!$checkExist->execute()) { die "Error: ". $checkExist->errstr ."\n"; } $checkExist->finish(); $dbh->disconnect(); } # End of else $dbh = DBI->connect("dbi:mysql:".$db.":".$host.":".$port."", "".$user."", "".$pass."", { 'PrintError' => 1, 'RaiseError' => 1 } ) or die "Could not connect to ".$host.": ". $DBI::errstr ."\n"; my $tables = $dbh->do("SHOW TABLES FROM `".$db."` WHERE Tables_in_ +".$db." LIKE '".$table."'") or die "Error: " .dbh->errstr. "\n"; if ($tables eq 1) { printf "Table: ".$table." exists not creating: ".$table."\n"; } else { printf "Table: ".$table." does not exist creating: ".$table."\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"; } $checkExist->finish(); $dbh->disconnect(); } # End of else my $range = 50; my $minimum = 100; my $random_number = int(rand($range)) + $minimum; my $time = time(); my $losses = $time . ' ' . $random_number; $dbh = DBI->connect("dbi:mysql:".$db.":".$host.":".$port."", "".$user."", "".$pass."", { 'PrintError' => 1, 'RaiseError' => 1 } ) or die "Could not connect to ".$host.": ". $DBI::errstr ."\n"; $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 "Added:" .$output. "\n";

Seeking for Perl wisdom...on the process...not there...yet!

In reply to MySQL avoid multiple connect request by thanos1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.