Introduction
This tutorial was written with two types of people in mind:
- Python programmers who want to access DB2 V8.1 databases from their Python programs.
- Database developers who are interested in Python and want to see how it works with DB2.
Python is a great tool to use with DB2. It combines the ability to quickly and simply access a DB2 database with the power to perform more complex tasks that require a general purpose programming language. This tutorial shows you everything you need to know to interact with DB2 V8.1 using the Python programming language. In particular, you'll learn about the Python DB2 module, which allows you to connect to a DB2 database, execute queries, fetch results, and commit or rollback transactions. To keep things as simple as possible, you won't have to build an entire application or write a lot of code. Instead, you'll be shown short, simple examples that can be run directly in Python using the sample database that comes with DB2. By the end of the tutorial you will have received a thorough introduction to the entire Python DB2 interface.
To get the most out of this tutorial, you should be familiar with basic relational database concepts and SQL, and have some programming knowledge. Experience with DB2 and Python would be helpful, but are not essential. If you can formulate an SQL query and understand basic programming logic, you should be able to follow this tutorial.
This tutorial focuses on using the Python DB2 module to access IBM's DB2 database management system (DBMS). With the Python DB2 module you can write programs that access, display, and update the information in a DB2 database. By the end of the tutorial you should be comfortable interacting with a DB2 database using Python.
Specifically, this tutorial will show you how to:
- Connect to a DB2 database
- Create cursors
- Execute queries and other SQL statements
- Interact with and display result sets
- Commit and rollback transactions
- Close down cursors and connections
This tutorial uses the following software. Make sure they are installed and running correctly before starting this tutorial:
- DB2 UDB Version 8.1. You can download a DB2 from the developerWorks downloads page. The tutorial uses the SAMPLE database that is created during a typical DB2 installation.
- Python (version 2.2 or higher is recommended).
- PyDB2 Package, which contains the Python DB2 interface module and utilities. Also see Project: PyDB2, which contains additional information about the package.
Over the course of the tutorial we will explore the entire database API and some additional utilities. Along the way you will be given code examples that you can execute yourself. All the examples are demonstrated using Python's interactive mode in order to illustrate the results of each command. The tutorial examples were tested against DB2 UDB 8.1 Enterprise Edition for Linux, using the SAMPLE database. Specifically, DB2 8.1 and Python 2.2.2 were installed on Mandrake 9.0. The techniques shown in this tutorial should work equally well with previous versions of DB2, as well as versions running on other platforms, including RedHat 7.2 or higher, SuSE 7.3 or higher, and SCO Linux 4.0 or higher.
Note that the DB2 UDB SAMPLE database is not automatically installed when you install DB2. You can install the SAMPLE database using the First Steps graphical utility, or the db2sampl
command line utility. See Resources for detailed information on DB2 Information Center.

