Other Applications 43
for albumInfo in albums:
currentAlbum = album(albumInfo)
tableRows.append(currentAlbum.tableRow())
table = "\n"
\n"table += "\n".join(tableRows)table += "
else:
table = "Unable to acquire albums
\n"
else:
table = "Unable to connect to database " + self.db + ".
\n";
return table
def page(self):
page = "\n"
page += "My Albums \n"
page += "\n"
page += "My Albums
\n"
page += self.table()
page += "\n"
page += "\n"
return page
albumList = albums( U
SERNAME
, P
ASSWORD
)
print albumList.page()
Perl
Perl requires the DBI database interface to connect to MySQL. You can get it from CPAN:
perl MCPAN e shell
install dbi
install DBD::mysql
This will get you the DBI interface and the MySQL information it needs to work.
#!/usr/bin/perl
use DBI;
$driver = "mysql";
$host = "localhost";
$port = 3306;
$user = "U
SERNAME
";
$pass = "P
ASSWORD
";
$database = "music";
$query = "SELECT album, artist, year, rating FROM albums ORDER BY artist";
$dataSource = "DBI:$driver:database=$database;host=$host;port=$port";
print "\n";
print "My Albums \n";
print "\n";
print "My Albums
\n";
print "\n";
print "\n"; Album Artist Year Rating if ($databaseHandle = DBI >connect($dataSource, $user, $pass)) {if ($queryHandle = $databaseHandle >prepare($query)) {if ($queryHandle >execute) {while ($rowHandle = $queryHandle >fetchrow_hashref()) {