This short little sample program produces the results below:
import ibm_db
import ibm_db_dbi
database = "xxx"
user = "xxx"
password = "xxx"
ibm_db_conn = ibm_db.connect(database,user,password)
conn = ibm_db_dbi.Connection(ibm_db_conn)
sql = "select ptptno from mpms99.mpat where ptptno like '%T%'"
cursor = conn.cursor()
cursor.execute(sql)
for row in cursor.fetchall():
print(row)
cursor.close()
conn.close()
Results:
python3 ./testibm_db.py
(' TEST',)
(' TRMC',)
(' ATHENA',)
(' TESTNS',)
(' Testdem',)
(' Testing',)
('ACA-RETRO',)
('ELTIV ',)
('UNIDENTIF',)
Kevin Bucknum
Senior Programmer Analyst
MEDDATA/MEDTRON
Tel: 985-893-2550
-----Original Message-----
From: OpenSource [mailto:opensource-bounces@xxxxxxxxxxxx] On Behalf Of
John Yeung
Sent: Thursday, July 07, 2016 12:15 PM
To: IBMi Open Source Roundtable
Subject: Re: [IBMiOSS] IBM i python ibm_db_dbi example usage
On Wed, Jul 6, 2016 at 3:59 PM, Jack Callahan <jjcllhn@xxxxxxxxx> wrote:
Can anyone point me to a decent example that connects to the local
IBMi DB2 using ibm_db_dbi, and iterates over the result set? Looking
for help mapping the connect method parameters.
That's the key. Once connected, Bob's your uncle. But how do you
connect?
Right now, I'm in the same boat as you, using a Python Litmis Space.
John Yueng provided a nice example for iSeriesPython on his blog, but
that uses db2 rather than ibm_db
http://iseriespython.blogspot.ca/2013/05/copy-physical-file-to-excel-i
n-9-lines.html
Thanks for the plug. iSeriesPython makes it simple whether you're using
db2 or ibm_db_dbi. As you've seen from the blog post,
db2.connect() doesn't require any parameters at all, if you're happy
with the default, which (sensibly enough) is to connect to the local
database using the credentials you're already logged in with.
The ibm_db_dbi that comes with iSeriesPython is not much more
complicated:
connection = ibm_db_dbi.connect('*LOCAL')
While it's not needed, you could also use the same parameter for db2:
connection = db2.connect('*LOCAL')
But IBM's Python for PASE is not quite so simple. At least, I have not
yet been able to figure out the magic incantation to connect.
Hopefully Aaron or Kevin can chime in here.
John Y.
--
This is the IBMi Open Source Roundtable (OpenSource) mailing list To
post a message email: OpenSource@xxxxxxxxxxxx To subscribe, unsubscribe,
or change list options,
visit:
http://lists.midrange.com/mailman/listinfo/opensource
or email: OpenSource-request@xxxxxxxxxxxx Before posting, please take a
moment to review the archives at
http://archive.midrange.com/opensource.
As an Amazon Associate we earn from qualifying purchases.