Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
dws is quite right. I agree, one gets bored typing in all that stuff. I have a few subroutines in a local module that I use with pretty much anything I write:
package GS::DBIUtilities; require Exporter; @ISA = ("Exporter"); use DBI; my $module_dbh; sub GetDBH { #-------------------------------------------------------------- # Connect to the amt db and return a db #-------------------------------------------------------------- my $db = shift; $module_dbh = DBI->connect("DBI:mysql:database=$db", "xxxxx", "xxx +xx"); return $module_dbh; } sub DropDBH { #--------------------------------------------------------------- # Disconnect from DB #--------------------------------------------------------------- $module_dbh->disconnect if $module_dbh; } sub InsertMultipleValues { #--------------------------------------------------------------- # Inserts contents of a hashref into the db table specified #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $Inserts = shift; my @cols = keys %$Inserts; my @vals = @$Inserts{@cols}; my $cols = join ',', @cols; my $places = '?,' x @vals; chop $places; my $sth = $dbh->prepare("INSERT INTO $table ($cols) VALUES ($place +s)") or die $dbh->errstr; $sth->execute(@vals) or die "$dbh->errstr : $table"; } sub ReplaceMultipleValues { #--------------------------------------------------------------- # Replaces contents of a hashref into the db table specified #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $Replaces = shift; my @cols = keys %$Replaces; my @vals = @$Replaces{@cols}; my $cols = join ',', @cols; my $places = '?,' x @vals; chop $places; my $sth = $dbh->prepare("REPLACE INTO $table ($cols) VALUES ($plac +es)") or die $dbh->errstr; $sth->execute(@vals) or die $dbh->errstr; } sub FetchSingleItem { #--------------------------------------------------------------- # Fetch a single item from a database #--------------------------------------------------------------- my $dbh = shift; my $FetchCol = shift; my $table = shift; my $SearchCol = shift; my $SearchVal = shift; my $sth = $dbh->prepare("SELECT $FetchCol FROM $table WHERE $Searc +hCol = ? LIMIT 1") or die $dbh->errstr; $sth->execute($SearchVal); my @ref = $sth->fetchrow_array; return $ref[0]; } sub InsertAndGetID { #--------------------------------------------------------------- # inserts an entry into a db and gets the auto_increment ID #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $Inserts = shift; my $IDCol = shift; $Inserts->{$IDCol} = 'NULL'; $dbh->do("LOCK TABLES $table WRITE") or die $dbh->errstr; InsertMultipleValues($dbh,$table,$Inserts); $sth = $dbh->prepare("SELECT LAST_INSERT_ID() FROM $table") or die + $dbh->errstr; $sth->execute or die $dbh->errstr; my @ary = $sth->fetchrow_array or die $dbh->errstr; $dbh->do("UNLOCK TABLES") or die $dbh->errstr; $sth->finish; return $ary[0]; } sub FetchStar { #--------------------------------------------------------------- # Retrieves the whole of each row that matches the submitted # criteria. Returns a hashref if there is only one row, # otherwise a ref to an array of hashes. #--------------------------------------------------------------- my $dbh = shift; my $table = shift; my $SearchCol = shift; my $SearchVal = shift; my $sth = $dbh->prepare("SELECT * FROM $table WHERE $SearchCol = ? +") or die $dbh->errstr; $sth->execute($SearchVal); my @returns; while (my $ref = $sth->fetchrow_hashref) { push @returns, $ref; } if (@returns <= 1) { return $returns[0]; } else { return \@returns; } } @EXPORT = qw/ GetDBH DropDBH InsertMultipleValues ReplaceMultipleValues FetchStar FetchSingleItem InsertAndGetID /; 1;#


George Sherston

In reply to Re: MySQL and Perl - Shorthand by George_Sherston
in thread MySQL and Perl - Shorthand by mt2k

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (3)
    As of 2014-09-21 15:44 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (172 votes), past polls