Warning

This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 44: Using Python's Database API

Python has a standardized database API that enables you to use the same code to access multiple databases. Each database you wish to connect to has a different module that knows how to talk to that database and follows the standard in the https://www.python.org/dev/peps/pep-0249/ PEP. This makes it easier to work with databases that all have different APIs for accessing them. For this exercise you'll be using the sqlite3 module found at https://docs.python.org/2/library/sqlite3.html to work with SQL.

Learning An API

One thing you will constantly have to do as a programmer is learn APIs written by other people. I haven't specifically covered the most efficient way to do this because it's something that most programmers simply pick up by way of learning a language. The Python language and its modules are intertwined so closely that you can't help but learn the APIs in these modules when you learn Python. However, there is an efficient way to learn APIs that I use, and in this exercise you're going to learn it.

To learn an API like the sqlite3 module I would do this:

  1. Find all the documentation for the API, and if there is no documentation then find the code.
  2. Review the samples or test code and replicate them in my own files. Reading them usually isn't enough. I will actually get them working because, guess what, many times the documentation doesn't work with the current version of the API. Making everything in the documentation work will help me find all the things they forgot to mention.
  3. Take notes on any Works For Me (WFM) situations as you work on getting the sample code to work on my machine. A WFM is where the person writing the documentation left out significant configuration steps because their computer is already configured. Most programmers who write documentation don't start with a fresh machine, so they leave out libraries and software they installed that nobody else has. These WFM gaps will bite you later when you try to set up the API in production, so I note them for later.
  4. Make flash cards or notes for all of the main API entry points and what they do.
  5. Attempt to write a small test spike that uses the API but using only your notes. If you hit a part of the API you don't remember, jump back to the documentation and update your notes.
  6. Finally, if the API is difficult to use, I'll consider "wrapping" it with a simple API that does only the things I need so I can forget about it.

If doing this doesn't teach you the API, then you should consider finding a different one to use. If the author of the API tells you to "read the code", there is most likely another project that has documentation. Go use that project. If you must use this API, then consider taking your notes based on their code and writing a book that you sell to make money off the author's laziness.

Exercise Challenge

You are to study the sqlite3 API in this way and then attempt to write your own database simplification API. Keep in mind that the DB API 2.0 is already a nice simple API for accessing a database, so you're only practicing having to wrap a terrible API. Your goal should be to learn the sqlite3 API well enough to then devise a simpler way to access it.

Sometimes "simpler" is purely subjective or based on current needs. You could decide that what you need to simplify is not how to talk to a SQL database, but rather how you need to talk to a SQL database. If your application only needs to deal with people and pets then your simplification could simply be making an API that is only for you.

Further Study

Read the documentation for other database APIs in Python. You can read the Pyscopg PostgreSQL API, and also the MySQL Python Driver.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.

$29.00

Buy the Online Course

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.