简体   繁体   English

Python 3和IBM_DB设置问题

[英]Python 3 and IBM_DB Setup Issues

I am currently using a Windows 10 machine and Python 3.6.3 and have downloaded and installed the requirements listed here: https://www.ibm.com/support/knowledgecenter/en/SSSNY3_10.1.0/com.ibm.swg.im.dbclient.python.doc/doc/t0054367.html from the following link: 我当前正在使用Windows 10计算机和Python 3.6.3,并已下载并安装了此处列出的要求: https : //www.ibm.com/support/knowledgecenter/zh-CN/SSSNY3_10.1.0/com.ibm.swg.im来自以下链接的.dbclient.python.doc / doc / t0054367.html

https://www.ibm.com/support/knowledgecenter/en/SSSNY3_10.1.0/com.ibm.swg.im.dbclient.python.doc/doc/r0054697.html https://www.ibm.com/support/knowledgecenter/zh-CN/SSSNY3_10.1.0/com.ibm.swg.im.dbclient.python.doc/doc/r0054697.html

I have installed the ODBC driver (IBM DB2 ODBC Driver) and configured it in my ODBC Data Source Administrator interface on the Windows 10 machine. 我已经安装了ODBC驱动程序(IBM DB2 ODBC驱动程序),并在Windows 10计算机上的ODBC数据源管理员界面中对其进行了配置。 I have also done this for a SQL Server connection using the appropriate set up for that. 我还使用适当的设置对SQL Server连接进行了此操作。

The issue I have is that I am receiving the following error: 我遇到的问题是我收到以下错误:

Traceback (most recent call last):
  File "C:\Users\<Username>\Python\Test IBM Connection.py", line 6, in <module>
    ibm_db.connect("<DSN>","<Username>","<Password>")
Exception: [IBM][CLI Driver] SQL1531N  The connection failed because the name
specified with the DSN connection string keyword could not be found in either 
the db2dsdriver.cfg configuration file or the db2cli.ini configuration file.  
Data source name specified in the connection string: "<DSN>".
 SQLCODE=-1531

I, of course have googled this issue to no avail. 我当然用谷歌搜索这个问题都没有用。 I am new to IBM_DB and it requirements and need help configuring the DSN to work properly. 我是IBM_DB的新手,它需要并且需要帮助来配置DSN以使其正常工作。

I have also attempted to run the db2dsdcfgfill - but have been unsuccessful, see here: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_9.5.0/com.ibm.swg.im.dbclient.config.doc/doc/r0054557.html 我也尝试运行db2dsdcfgfill-但未成功,请参见此处: https ://www.ibm.com/support/knowledgecenter/zh/SSEPGG_9.5.0/com.ibm.swg.im.dbclient.config.doc /doc/r0054557.html

I am unsure what information needs to be in these files, and where they need to be and how to register them so the system is seeing them. 我不确定这些文件中需要包含什么信息,它们应该位于何处以及如何注册它们,以便系统能够看到它们。

I have been able to connect and query an SQL Server, but have not been able to connect to the IBM iSeries AS400 system. 我已经能够连接和查询SQL Server,但无法连接到IBM iSeries AS400系统。

I have researched the many parts of the issue I am having but have not found any solutions for my issue. 我已经研究了该问题的许多部分,但没有找到解决该问题的任何方法。

As requested I have added the following information: 根据要求,我添加了以下信息:

Connection String: 连接字符串:

ibm.connect("<DSN>", "<Username>","<Password>")

ODBC Version is 11.01.00.1527 ODBC版本是11.01.00.1527

I am connecting to an IBM iSeries database - it running on system i and is running version 7.2 我正在连接到IBM iSeries数据库-它在系统i上运行并且正在运行7.2版

This answer shows some examples of how to use db2cli.exe to configure db2dsdriver.cfg 此答案显示了一些有关如何使用db2cli.exe来配置db2dsdriver.cfg的示例。

Remember that you do not need to use DSNs in python to connect to Db2 databases, you can use the long form of the connect to specify all the details of the connection. 请记住,您不需要在python中使用DSN来连接到Db2数据库,您可以使用长连接形式来指定连接的所有详细信息。 Using a DSN is useful when the DSN is used by multiple different local applications(so the config is only in one place), or when your python scripts need to run on multiple environments which may use different DSN details without code changes and where the DSNs are under control of a different group, or when DSN-parameters are environment specific. 当多个不同的本地应用程序使用DSN(因此配置仅在一个位置)时,或者当您的python脚本需要在可能使用不同DSN详细信息而无需更改代码且DSN所在的多个环境中运行时,使用DSN很有用。处于不同组的控制之下,或者DSN参数是特定于环境的。 So choose the best approach to suit your enterprise. 因此,选择适合您企业的最佳方法。

If you must use DSNs, choose either user-dsn's or system-dsn as appropriate for your environment and requirements. 如果必须使用DSN,请根据您的环境和要求选择user-dsn或system-dsn。

Choose which format of configuration file to use for the Db2 client. 选择用于Db2客户端的配置文件格式。 The db2cli.ini is an old legacy format Windows style INI file with x=y tuples and [section] blocks in plain text. db2cli.ini是一个旧的旧格式Windows样式INI文件,带有x = y元组和[section]块为纯文本。 You can manipulate its contents either with a text editor or with command line commands of Db2 . 您可以使用文本编辑器或Db2的命令行命令来操纵其内容。 This old format has enough online documentation and has been around for decades, so I won't expand on this. 这种旧格式具有足够的在线文档,并且已经存在数十年了,因此我将不对此进行扩展。

The db2dsdriver.cfg file is a more recent format. db2dsdriver.cfg文件是较新的格式。 It is an XML file. 这是一个XML文件。 You can edit it with Db2 commands (db2cli.exe on Windows) or via a text editor (or XML editor). 您可以使用Db2命令(在Windows上为db2cli.exe)或通过文本编辑器(或XML编辑器)对其进行编辑。 I would use this format for new environments with currently supported Db2 clients. 我会将这种格式用于具有当前支持的Db2客户端的新环境。 As its XML, there is a validation step (db2cli validate ...). 作为其XML,有一个验证步骤(db2cli validate ...)。

The online Db2 Knowledge centre has all this information although you do need to spend time studying many pages and rehearse and test carefully. 在线Db2知识中心提供了所有这些信息,尽管您确实需要花时间研究许多页面并仔细排练和测试。

Here are some sample lines of batch file script, which you can run in an ADMINISTRATOR db2cmd.exe window on MS-windows, which show how to use the db2cli.exe tool to populate db2dsdriver.cfg. 以下是批处理文件脚本的一些示例行,您可以在MS-windows的ADMINISTRATOR db2cmd.exe窗口中运行这些示例行,它们显示了如何使用db2cli.exe工具来填充db2dsdriver.cfg。 You have to have at least local-admin rights to run these commands. 您必须至少具有本地管理员权限才能运行这些命令。

First set some variables to match your specific environment, port-number, hostname of Db2 server, credentials and DSN and remote database name (these 2 can be different values). 首先设置一些变量以匹配您的特定环境,端口号,Db2服务器的主机名,凭据和DSN以及远程数据库名称(这两个可以是不同的值)。 Then run db2cli to write entries to the db2dsdriver.cfg using the variables previously set, and finally run db2cli to attempt to connect to the DSN. 然后运行db2cli以使用先前设置的变量将条目写入db2dsdriver.cfg,最后运行db2cli尝试尝试连接到DSN。 These are trivial examples, in real cases you often need to add many additional parameters to the database section or DSN section (Db2 on different platforms supports many options that you can set at connection time). 这些是简单的示例,在实际情况下,您通常需要向数据库部分或DSN部分添加许多其他参数(不同平台上的Db2支持许多可以在连接时设置的选项)。 You set any of these via the db2cli writecfg add syntax, although IBM's documentation is less than helpful in this area. 您可以通过db2cli writecfg add语法设置其中的任何一个,尽管IBM的文档在这方面没有多大帮助。 These examples assume your db2dsdriver.cfg is currently empty. 这些示例假定您的db2dsdriver.cfg当前为空。 You may need additional parameters for connecting to Db2 for i. 您可能需要其他参数才能连接到i的Db2。 My examples are for Db2 for LUW. 我的示例适用于LUW的Db2。 You also need relevant licences. 您还需要相关的许可证。 Save the example below to a batch file, change the values to suit your environment, and rehearse on a development or test environment. 将下面的示例保存到批处理文件中,更改值以适合您的环境,然后在开发或测试环境中进行练习。

set db2_port=port_number_on_which_db2_is_listening

set db2_server=ip_address_or_fqdn_of_db2_server

set db2_id_to_connect=myusername

set db2_id_pwd=password

set dsn_name=sample

set db_name=sample


db2cli writecfg add -dsn %dsn_name% -database %db_name% -host %db2_server% -port %db2_port%  


db2cli writecfg add -database %db_name% -host %db2_server% -port %db2_port% 

db2cli validate -dsn %dsn_name% -connect -user %db2_id_to_connect% -passwd %db2_id_pwd% 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM