Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Using Python to access DB2 for Linux

Open source programming, serious databases

Python expert and jack-of-all-trades technologist, Patrick O'Brien, a Studio B client, has been developing software for over 15 years, beginning with MS DOS, then Windows, and now Linux. His very first application used Paradox for DOS, and he has been using databases to solve data management problems ever since. Patrick is actively involved in several Open Source projects that use the Python programming language. He is the author of PyCrust, an interactive, graphical Python shell written in Python using the wxPython GUI toolkit, a developer of the PythonCard wxPython application construction kit, and leader of the PyPerSyst team, which is working on several object persistence mechanisms for Python applications. His Python articles have been published by O'Reilly and IBM, and he has presented at the O'Reilly Open Source Convention. You can contact Patrick at pobrien@orbtech.com.

Summary:  The Python Database API is a standard specification for the implementation of a Python interface to a database management system. The DB2 module is an implementation of the Python Database standard. This tutorial covers the entire API and provides sample code that can be run in Python's interactive mode. It uses the sample database that ships with DB2 UDB 8.1 Enterprise Edition for Linux.

Date:  17 Dec 2002
Level:  Intermediate PDF:  A4 and Letter (91 KB | 26 pages)Get Adobe® Reader®

Activity:  13798 views
Comments:  

Introduction

Should I take this tutorial?

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.


What is this tutorial about?

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

Tools

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.

1 of 7 | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Information Management, Linux
ArticleID=139667
TutorialTitle=Using Python to access DB2 for Linux
publish-date=12172002
author1-email=pobrien@orbtech.com
author1-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Try IBM PureSystems. No charge.