This code just open a connection to a mysql database, and then it close it:
#!/usr/bin/perl
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect("dbi:mysql:test", "root", "password") ||
die "Connection error: $DBI::errstr\n";
$dbh->disconnect();
print "Done.";
There is something interesting even in these few lines.
First of all we see that to start a connection we call the connect() function in the DBI package passing three paramenters representing the database to which we are about to connect (protocol and database name), the database user and its password.
If connect() fails, we let our script die, showing the error message that is stored in DBI::errstr.
Finally (!) we close the connection calling the disconnect() method on the dbh object resulting by the connect() call.
No comments:
Post a Comment