IBM Support

Python script accessing DB2 data - Simple example

How To


Summary

The IBM i platform has added Open Source Packages to the system to allow developers to run common open source software on their system.
This document is a simple example of installing Python and using it to query Db2 for i.
It is loosely excerpted from "Breaking out of PASE: Accessing the rest of your IBM i from Python"

Environment

While the ACS product contains the functionality to allow IBM i administrators to easily install open source software on their systems, the open source software itself is beyond the scope of a Support Line contract. Please see Open Source Support for IBM i.

Steps

1) Install IBM i Access Client Solutions on a client PC and configure a connection to an IBM i system.

2) Within the ACS console, access the "Tools" pull-down and select "Open Source Package Management"

3) Install packages python3; python3-pip; python3-ibm_db; and python3-itoolkit .  If no open source packages have been installed on your system, the installation of these packages will likely pull in additional prerequisite packages.

4) From the ACS console, open an SSH Terminal to the same IBM i.

5) Install the PrettyTable python library:

     $ pip3 install PTable

6) Create a file with an extension of ".py" and use a text editor to add the following source code

 from prettytable import from_db_cursor
 import ibm_db_dbi as db2
 conn = db2.connect()
 cur = conn.cursor()
 cur.execute("select * from qiws.qcustcdt")
     print(from_db_cursor(cur))

7) Execute the script:

     $ python3 kpretty.py

Output:

marquis@MyIBMi:~/scripts$ python3 kpretty.py
+--------+----------+------+---------------+--------+-------+--------+--------+--------+---------+--------+
| CUSNUM |  LSTNAM  | INIT |     STREET    |  CITY  | STATE | ZIPCOD | CDTLMT | CHGCOD |  BALDUE | CDTDUE |
+--------+----------+------+---------------+--------+-------+--------+--------+--------+---------+--------+
| 938472 | Henning  | G K  | 4859 Elm Ave  | Dallas |   TX  | 75217  |  5000  |   3    |  37.00  |  0.00  |
| 839283 | Jones    | B D  | 21B NW 135 St | Clay   |   NY  | 13041  |  400   |   1    |  100.00 |  0.00  |
| 392859 | Vine     | S S  | PO Box 79     | Broton |   VT  |  5046  |  700   |   1    |  439.00 |  0.00  |
| 938485 | Johnson  | J A  | 3 Alpine Way  | Helen  |   GA  | 30545  |  9999  |   2    | 3987.50 | 33.50  |
| 397267 | Tyron    | W E  | 13 Myrtle Dr  | Hector |   NY  | 14841  |  1000  |   1    |   0.00  |  0.00  |
| 389572 | Stevens  | K L  | 208 Snow Pass | Denver |   CO  | 80226  |  400   |   1    |  58.75  |  1.50  |
| 846283 | Alison   | J S  | 787 Lake Dr   | Isle   |   MN  | 56342  |  5000  |   3    |  10.00  |  0.00  |
| 475938 | Doe      | J W  | 59 Archer Rd  | Sutter |   CA  | 95685  |  700   |   2    |  250.00 | 100.00 |
| 693829 | Thomas   | A N  | 3 Dove Circle | Casper |   WY  | 82609  |  9999  |   2    |   0.00  |  0.00  |
| 593029 | Williams | E D  | 485 SE 2 Ave  | Dallas |   TX  | 75218  |  200   |   1    |  25.00  |  0.00  |
| 192837 | Lee      | F L  | 5963 Oak St   | Hector |   NY  | 14841  |  700   |   2    |  489.50 |  0.50  |
| 583990 | Abraham  | M T  | 392 Mill St   | Isle   |   MN  | 56342  |  9999  |   3    |  500.00 |  0.00  |
+--------+----------+------+---------------+--------+-------+--------+--------+--------+---------+--------+


[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG60","label":"IBM i"},"Component":"","Platform":[{"code":"PF012","label":"IBM i"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB57","label":"Power"}}]

Document Information

More support for:
IBM i

Software version:
All Versions

Operating system(s):
IBM i

Document number:
791899

Modified date:
10 May 2022

UID

ibm10791899

Manage My Notification Subscriptions