How to connect Python to Db2

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

Is there a way to connect Python to Db2?

10条回答
  •  天命终不由人
    2020-12-04 20:03

    After lots of digging I discovered how to connect with DB2 using ibm_db.

    First off, if you use a python version higher than 3.2 use

    pip install ibm_db==2.0.8a

    version 2.0.8 (the latest) will fail to install.

    then use the following to connect

    import ibm_db_dbi as db
    
    conn = db.connect("DATABASE=name;HOSTNAME=host;PORT=60000;PROTOCOL=TCPIP;UID=username;PWD=password;", "", "")
    

    list tables with

    for t in conn.tables():
        print(t)
    

    and execute SQL with

    cursor = conn.cursor()
    cursor.execute("SELECT * FROM Schema.Table")
    for r in cursor.fetchall():
        print(r)
    

    check this link for official not so accurate documentation

提交回复
热议问题