1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

IBM Cloud functions(python)からDB2 on Cloudへ接続する方法

Last updated at Posted at 2019-11-28

IBM Cloud functions(node.js)からDB2 on Cloudへ接続する方法 のpython版です。この記事ではpython3.7でDb2 on Cloudへの接続を試してみたいと思います。
といっても、Create Action時にnode.jsではなくpythonを選ぶだけですが・・・
pythonを使ったDb2への接続方法の資料としては、以下が参考になります。
https://github.com/ibmdb/python-ibmdb/wiki/APIs#ibm_dbcallproc

Db2OnCloudへの接続サンプルコード
import sys
import ibm_db

def main(dict):
    ssldsn = "DATABASE=xxxx;HOSTNAME=xxxxxx;PORT=xxxxx;PROTOCOL=TCPIP;UID=xxxxx;PWD=xxxxxx;Security=SSL;"
    conn=ibm_db.connect(ssldsn,'','')
    
    #Client Information
    client = ibm_db.client_info(conn)
    if client:
        print("DRIVER_NAME: %s" % client.DRIVER_NAME)
        print("DRIVER_VER:  %s" % client.DRIVER_VER)
        print("DATA_SOURCE_NAME: %s" % client.DATA_SOURCE_NAME)
        print("DRIVER_ODBC_VER: %s" % client.DRIVER_ODBC_VER)
        print("ODBC_VER: %s" % client.ODBC_VER)
        print("ODBC_SQL_CONFORMANCE:  %s" % client.ODBC_SQL_CONFORMANCE)
        print("APPL_CODEPAGE: %s)" % client.APPL_CODEPAGE)
        print("CONN_CODEPAGE: %s)" % client.CONN_CODEPAGE)
    else:
        print("client information cannot be retrieved.")

    
    #Server Information
    server = ibm_db.server_info(conn)
    if server:
        print("DBMS_NAME: %s"  % server.DBMS_NAME)
        print("DBMS_VER: %s" % server.DBMS_VER)
        print("DB_NAME: %s" % server.DB_NAME)
        print("DB_CODEPAGE: %s"  % server.DB_CODEPAGE)
        print("DFT_ISOLATION: %s" % server.DFT_ISOLATION)
        print("IDENTIFIER_QUOTE_CHAR: %s" % server.IDENTIFIER_QUOTE_CHAR)
        print("INST_NAME: %s" % server.INST_NAME)
    else:
        print("server information cannot be retrieved.")
    
    #SQL statements
    sql = "SELECT DBPARTITIONNUM, REG_VAR_NAME, REG_VAR_VALUE from SYSIBMADM.REG_VARIABLES"
    stmt = ibm_db.prepare(conn, sql,)
    result = ibm_db.execute(stmt)
    row = ibm_db.fetch_both(stmt)
    while ( row ):
        print("%-5d"  % row['DBPARTITIONNUM'])
        print("%-30s" % row['REG_VAR_NAME'])
        print("%-30s" % row['REG_VAR_VALUE'])
        row = ibm_db.fetch_both(stmt)
    
    
    ibm_db.close(conn)
    return { 'result': 'finished!' }
1
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?