Re: Fetch Data from MySQL
by zwon (Abbot) on Sep 21, 2011 at 16:15 UTC
|
It's quite simple. Standard way to access databases in perl is DBI module with appropriate DBD::* driver. Have a look onto DBD::mysql, its documentation contains example.
| [reply] |
Re: Fetch Data from MySQL
by runrig (Abbot) on Sep 21, 2011 at 16:17 UTC
|
| [reply] |
Re: Fetch Data from MySQL
by zentara (Cardinal) on Sep 21, 2011 at 17:48 UTC
|
Here is your basic example, setting up and populating the test data table is up to you. You must have DBI and DBD::Mysql installed. DBI will find all installed sql drivers automatically
#!/usr/bin/perl
use warnings;
use strict;
use DBI;
# Gather some information from the user :
print "Enter your MySQL username: ";
my $username = <STDIN>;
chomp $username; # remove trailing newline from Enter keypress
print "Enter your MySQL password; ";
my $password = <STDIN>;
chomp $password;
# Connect to the database
my $dbh = DBI->connect("DBI:mysql:database=perl:host=localhost",$usern
+ame,$password) || die "Couldn't connect to database";
print "Database connection established.";
# Prepare statement
my $sth = $dbh->prepare( "SELECT * FROM perl_table" );
# Send statement to database
$sth->execute || die "Couldn't execute statement";
# Print out the data
my @row;
while ( @row = $sth->fetchrow_array() ) {
print "@row \n";
}
# One more thing: we need to disconnect from the database server
# when we're done.
$dbh->disconnect;
| [reply] [d/l] |
Re: Fetch Data from MySQL
by tospo (Hermit) on Sep 22, 2011 at 08:24 UTC
|
Yes, this is standard stuff. Apart from the documentation for the DBI module, you might also want to get "Programming the Perl DBI" from O'Reilly. It is quite old now but if you use simple DBI then it still tells you all you need to know about it and it is well written and has easy examples.
This may be all you need for a simple project but be aware of the fact that there are newer approaches to database interaction around, such as object-relational mapping, e.g. with DBIC which allow you to interact with a database without writing SQL queries. IMHO, if you are just getting started with this, you might as well learn DBIC instead of bare-bones DBI. | [reply] |
|
Ugh. Please don't recommend any of the object-relational mappers as a way around learning SQL. All ORMs I've seen so far allow you to avoid writing simple SQL statements, but horribly fail once your needs go beyond fetching single rows. Some ORMs allow you to define how to join tables together, but they all fail once your needs go towards more complex statements.
In addition, ORMs are the wrong way to think about your data. They make you think of aggregates over your data in terms of Perl loops instead of pushing the work down to the database. Also, they either force you to write a separate class representing a query result or think that all results you will ever fetch from the database correspond to a single row in one table. Neither approach makes much sense to me.
| [reply] |
|
Thank you. I'm glad I'm not the only one who doesn't see the point of working so hard to abstract away the SQL. DBIC is recommended so often that I keep looking at it, but it just looks like extra work. Writing and handling SQL queries -- especially ones simple enough for an ORM to handle -- isn't really that difficult.
| [reply] |
|
|
|
|
|