Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: MySQL and Perl - Shorthand

by George_Sherston (Vicar)
on Sep 21, 2002 at 12:42 UTC ( #199742=note: print w/ replies, xml ) Need Help??


in reply to MySQL and Perl - Shorthand

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


Comment on Re: MySQL and Perl - Shorthand
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://199742]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (9)
As of 2015-07-29 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (260 votes), past polls