iSeriesPython

General Category => General Discussion => Topic started by: joelj on November 28, 2018, 06:15:53 pm


Title: accessing ibm i database from Python
Post by: joelj on November 28, 2018, 06:15:53 pm
Good morning.

I have Python 3.7.1 installed on my windows 10 PC. I ma using Idle as editor. I am trying to do basic connection to an IBM i server.

import ibm_db
import ibm_db_dbi

database = "IBMINAME"
user = "JOEL"
password = "PASSWORD"

#ibm_db_conn = ibm_db.connect(database,user,password)
print("AA")

#ibm_db_conn = ibm_db.connect("DATABASE=IBMINAME;HOSTNAME=IBMINAME;PORT=8471;PROTOCOL=TCPIP;UID=JOEL;PWD=PASSWORD;", "", "")
#conn = ibm_db_dbi.Connection(ibm_db_conn)

conn = ibm_db_dbi.connect("DATABASE=IBMINAME;HOSTNAME=IBMINAME;PORT=8471;PROTOCOL=TCPIP;UID=JOEL;PWD=PASSWORD;", "", "")

print("BB")
sql = "select GFCUS from KFILFBI.GFPF"
cursor = conn.cursor()
cursor.execute(sql)
for row in cursor.fetchall():
    print(row)
    print("CC")
cursor.close()
conn.close()


I only get a printout of AA on my idle console. Any ideas why? There are no errors on the Idle console. When i use port 50000 or 60000, i get :

Exception: [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "xx.xxx.xxx.xxx".  Communication function detecting the error: "connect".  Protocol specific error code(s): "10061", "*", "*".  SQLSTATE=08001
 SQLCODE=-30081

Another thing, when i use port 8471, I do not get the SQL30081N  error above. 8471 is the port used by ibm database server jobs. But still, i only get AA, it looks like the code get stuck on the connection part, it doesnt get to the line where BB is supposed to be printed.

I tried adding firewall exceptions for port 8471 and 50000 60000, still same problem.

Do you have any ideas i can work on?

thanks,
Joel
Title: Re: accessing ibm i database from Python
Post by: joelj on November 29, 2018, 05:40:12 am
was able to make it work using pyodbc. very useful reference here:
https://ochiba77.blogspot.com/2011/09/how-to-connect-db2-with-python-pyodbc.html?showComment=1543491397417#c3112663716830198386

Many thanks to the Omi Chiba.