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
|