Putting it in Python
As with most programming languages, the more one knows about the data and data structure, the better one can program to handle it. This is at least doubly true for database programming with aggregate functions and clauses such as those which we cover in this chapter.
Putting the SELECT
statement into Python is not particularly complex, but handling the results intelligently requires a knowledge of their format. Again, setting up a basic database session in Python would look like this:
#!/usr/bin/env python import MySQLdb mydb = MySQLdb.connect('localhost', 'skipper', 'secret', 'sakila') cursor = mydb.cursor()
For the statement to be run against sakila
, we can use the following:
statement = """SELECT * FROM film WHERE title LIKE 'Z%' ORDER BY rating ASC"""
Then we execute it:
runit = cursor.execute(statement) fetch = cursor.fetchall()
If, at this point, we just print the value of fetch
, we get a lot of a database formatting:
print fetch ((999, 'ZOOLANDER...