使用Python连接db2

import ibm_db
connStr = "DATABASE=ODS;HOSTNAME=HOSTIP;PORT=50000;PROTOCOL=TCPIP;UID=USER;PWD=PASSWORD;"

conn = None
try:
    # 连接数据库
    conn = ibm_db.connect(connStr, "", "")
    sql = "SELECT * FROM UNITOLL.TB_SUBROAD"
    stmt = ibm_db.exec_immediate(conn, sql)
    result = ibm_db.fetch_both(stmt)    # 提取结果
    print(result)
    
except Exception as ex:
    print(ex)
finally:
    # 关闭数据库
    ibm_db.free_stmt(stmt)
    ibm_db.close(conn)

你可能感兴趣的:(使用Python连接db2)