How to connect Python to Db2

前端 未结 10 543
小鲜肉
小鲜肉 2020-12-04 19:53

Is there a way to connect Python to Db2?

10条回答
  •  鱼传尺愫
    2020-12-04 20:13

    You can connect to db2 from python using jaydeapi First install library running pip install jaydeapi download db2jcc4.jar Then you can connect using below code : by passing hostname,portno, userid,password database name

    import jaydebeapi
    
    conn_src = jaydebeapi.connect(
        'com.ibm.db2.jcc.DB2Driver',
        ['YourHostName:PortNo/DatabaseName','userid','password'],'C:/db2jcc4.jar'
    )
    
    cursor=conn_src.cursor()
    sql = 'Select * from schemaname.TableName fetch first 100 rows only '
    
    cursor.execute(sql)
    print("fetchall:")
    result = cursor.fetchall()
    for r in result:
        print(r)
    

提交回复
热议问题