Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: What use DBI?

by blue_cowdawg (Monsignor)
on Mar 12, 2013 at 20:33 UTC ( #1023046=note: print w/ replies, xml ) Need Help??


in reply to What use DBI?

      What are the benefits of using one of the DB modules like DBI as opposed to just making a mysql connection like this.

Are you kidding me? There's a whole ton of work you save yourself using DBI and the associated driver modules in the DBD:: family. I'm not even sure how your code shown in your post would even accomplish anything.

One typical example of code:

#!/usr/bin/perl -w use strict; use DBI; # connect to the database and create a handle to the connection. my $dbh=DBI->connect('DBI:mysql:host=myhost.domain.tld;database=mydata +base', 'user','password') or die $DBI::errstr; #create a statement handle and prep our SQL my $sth=$dbh->prepare(qq( select user_id,username,password,given,surname from users_table order +by user_id )) or die $dbh->errstr; $sth->execute(); while (my $row=$sth->fetchrow_arrayref){ # do something with it } | rest of code
In my mind and I'm sure I'm not alone using DBI is just basic to using databases with Perl. I am positive that without using DBI and writing code to scrape the output from a database the way you seem to describe would require a lot more code that my example above. You'd also run into lots of issues with syntax, quoting and other issues that might be difficult to track down and diagnose.

That reminds me: there's also the issue of portability. In 99% of the cases where I've written code that performs database operations I've been able to port it between database engines merely by switching which DBD driver I use. On rare occasion (using stored procedures for instance) I've had to change something up but that's rare.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg


Comment on Re: What use DBI?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-25 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls