42 Other
Python
Python requires the MySQLdb module. You can get it from the project's web page at
http://sourceforge.net/projects/mysql python/ if you do not already have it. You can find out
if you type python and then try to import MySQLdb . Python is case sensitive.
#!/usr/bin/python
import MySQLdb
class album:
def __init__(self, albumInfo):
self.album = albumInfo["album"]
self.artist = albumInfo["artist"]
self.year = albumInfo["year"]
self.rating = albumInfo["rating"]
def tableRow(self):
rowHTML = ""
for cell in [self.album, self.artist, self.year, self.rating]:
rowHTML += self.tableCell(cell)
rowHTML += "
\n"
return rowHTML
def tableHeader(self):
rowHTML = ""
for header in ["Album", "Artist", "Year", "Rating"]:
rowHTML += self.tableCell(header, "th")
rowHTML += "
\n"
return rowHTML
def tableCell(self, cellData, cellType="td"):
cellHTML = "<" + cellType + ">"
cellHTML += str(cellData)
cellHTML += "" + cellType + ">"
return cellHTML
class albums:
def __init__(self, user, password, host="localhost"):
self.db = music
self.query = SELECT album, artist, year, rating FROM albums ORDER BY artist
self.sql = MySQLdb.connect(host=host, user=user, passwd=password, db=self.db)
self.cursor = self.sql.cursor(MySQLdb.cursors.DictCursor)
self.nextAlbum = None
def __destroy__(self):
if self.sql:
self.cursor.close()
self.sql.close()
def table(self):
if self.sql:
albums = None
try:
self.cursor.execute(self.query)
albums = self.cursor.fetchall()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
if albums:
tableRows = [album.tableHeaders()]