Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

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 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 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:

  • Find all the documentation for the API, and if there is no documentation then find the code.

  • 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.

  • 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.

  • Make flash cards or notes for all of the main API entry points and what they do.

  • 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.

  • 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.

Previous Lesson Next Lesson

Register for Learn More Python the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.