DB2 Cheat Sheet (Instances)

Managing DB2 instances is a substantial portion of a DB2 DBA’s daily workload. Here’s a simple cheat sheet I’ve designed to define a DB2 instance and outline the basics of an instance’s memory model, process model, and file structure. I’ve also listed a few commands that are specific to instance management (creating, dropping, starting, stopping, etc).

Download

 

DB2 Cheat Sheet (Commands)

While preparing for a DB2 interview, I compiled a list of DB2 commands and grouped them by purpose. FYI, notice how DB2 system commands are essentially binaries (db2start, db2licm, db2ilist) and as a result are path dependent. On the other hand, Command Line Processor (CLP) commands are executed directly from a DB2 prompt or from the command line using db2 “execute some command”.

Download

Querying DB2 with Python

In this article, we’ll write a simple Python script that will query a DB2 database and print the results. To link Python and DB2, we’ll use the PyDB2 interface. To install PyDB2 download a copy and follow the instructions in the README file.

#!/usr/bin/env python

import DB2

# Instantiate connection object and connect to DB2 database
db = DB2.connect('dbname', 'username', 'password')

# Instantiate cursor object
cursor = db.cursor()

# Build sql statement using python's multiline quotation syntax
sql = """select address_line_1 , address_line_2,
        city, state, zip from property"""

try:

    # Execute statement and fetch results
    cursor.execute(sql)
    results = cursor.fetchall()

    # Loop through rows and print all columns
    for row in results:
        print row[0:]

except:
    print "Error: Unable to fetch data from DB2"

# Close database connection
db.close()

Continue reading