Chapter 7. Using a Database: Putting Python’s DB-API to Use

image with no caption

Storing data in a relational database system is handy.

In this chapter, you’ll learn how to write code that interacts with the popular MySQL database technology, using a generic database API called DB-API. The DB-API (which comes standard with every Python install) allows you to write code that is easily transferred from one database product to the next...assuming your database talks SQL. Although we’ll be using MySQL, there’s nothing stopping you from using your DB-API code with your favorite relational database, whatever it may be. Let’s see what’s involved in using a relational database with Python. There’s not a lot of new Python in this chapter, but using Python to talk to databases is a big deal, so it’s well worth learning.

Database-Enabling Your Webapp

The plan for this chapter is to get to the point where you can amend your webapp to store its log data in a database, as opposed to a text file, as was the case in the last chapter. The hope is that in doing so, you can then provide answers to the questions posed in the last chapter: How many requests have been responded to? What’s the most common list of letters? Which IP addresses are the requests coming from? Which browser is being used the most?

To get there, however, we need to decide on a database system to use. There are lots of choices here, and it would be easy ...

Get Head First Python, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.