【问题标题】:How to make connection in python to connect as400 and call any as400 programs with parameter如何在python中建立连接以连接as400并使用参数调用任何as400程序
【发布时间】:2017-11-30 07:30:17
【问题描述】:

任何人都知道如何在python中建立连接以连接as400系列系统并使用参数调用任何as400程序。

例如如何通过python连接as400来创建库。我想从 python 脚本中调用“CRTLIB LIB(TEST)”。

我可以通过pyodbc包连接到DB2数据库。

这是我连接 DB2 数据库的代码。

import pyodbc

connection = pyodbc.connect(
    driver='{iSeries Access ODBC Driver}',
    system='ip/hostname',
    uid='username',
    pwd='password')
c1 = connection.cursor()

c1.execute('select * from libname.filename')
for row in c1:
    print (row)

【问题讨论】:

    标签: python-3.x python-2.7 ibm-midrange db2-400 django-pyodbc


    【解决方案1】:

    如果您的 IBM i 设置为允许,您可以在 SQL 中使用CALL 调用QCMDEXC 存储过程。例如,

    c1.execute("call qcmdexc('crtlib lib(test)')")
    

    QCMDEXC 存储过程位于 QSYS2 中(实际的程序对象是 QSYS2/QCMDEXC1),其功能与位于 QSYS 中的熟悉的同名程序大致相同,但存储过程是专门用于调用的通过 SQL。

    当然,要使此示例正常运行,您的连接配置文件必须具有创建库的适当权限。

    也有可能您的 IBM i 没有设置为允许这样做。我不知道启用此功能的确切原因,但是在我工作的地方,我们有一个分区,上面显示的示例正常完成,而另一个分区则改为:

    pyodbc.Error: ('HY000', '[HY000] [IBM][System i Access ODBC Driver][DB2 for i5/OS]SQL0901 - SQL system error. (-901) (SQLExecDirectW)')
    

    【讨论】:

      【解决方案2】:

      此要点显示如何通过pyodbc 连接到 AS/400:

      https://gist.github.com/BietteMaxime/6cfd5b2dc2624c094575

      一些笔记;在本例中,SYSTEM 是您在with pyodbc.connect 语句中为 AS/400 设置的 DSN。您还可以通过以下修改将其切换为 SERVERPORT

      import pyodbc
      
      class CommitMode:
          NONE = 0  # Commit immediate (*NONE)  --> QSQCLIPKGN
          CS = 1  # Read committed (*CS)        --> QSQCLIPKGS
          CHG = 2  # Read uncommitted (*CHG)    --> QSQCLIPKGC
          ALL = 3  # Repeatable read (*ALL)     --> QSQCLIPKGA
          RR = 4  # Serializable (*RR)          --> QSQCLIPKGL
      
      class ConnectionType:
          READ_WRITE = 0 # Read/Write (all SQL statements allowed)
          READ_CALL = 1 # Read/Call (SELECT and CALL statements allowed)
          READ_ONLY = 2 # Read-only (SELECT statements only)
      
      def connstr(server, port, commit_mode=None, connection_type=None):
          _connstr = 'DRIVER=iSeries Access ODBC Driver;SERVER={server};PORT={port};SIGNON=4;CCSID=1208;TRANSLATE=1;'.format(
              server=server,
              port=port,
          )
          if commit_mode is not None:
              _connstr = _connstr + 'CommitMode=' + str(commit_mode) + ';'
          if connection_type is not None:
              _connstr = _connstr + 'ConnectionType=' + str(connection_type) + ';'
      
          return _connstr
      
      def main():
          with pyodbc.connect(connstr('myas400.server.com', '8471', CommitMode.CHG, ConnectionType.READ_ONLY)) as db:
              cursor = db.cursor()
              cursor.execute(
                  """
                  SELECT * FROM IASP.LIB.FILE
                  """
              )
              for row in cursor:
                  print(' '.join(map(str, row)))
      
      if __name__ == '__main__':
          main()
      

      我也清理了一些 PEP-8。祝你好运!

      【讨论】:

      • 这似乎是一种过于复杂的方式来完成 OP 已经提供的代码要做的事情,并且无法回答这个问题。
      • Anyone knows How to make connection in python to connect as400 iseries system 对我来说似乎是一个问题。我很久没有连接到 AS/400,但它不像连接到 SQL Server 那样简单。请注意,问题已被编辑 - 我最初没有从超大字体中看到“可以连接”。
      • 唯一的修改是添加了几个标签。问题正文仍然是原始形式,包含已成功连接并执行简单查询的代码。它就像连接到 SQL Server 或其他任何东西一样简单。看看 OP 的代码就知道了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2012-03-22
      相关资源
      最近更新 更多