如何在Linux上使用Python和Informix进行通信? [英] How do I get Python and Informix talking on Linux?

查看:208
本文介绍了如何在Linux上使用Python和Informix进行通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有一段时间了,尝试过来自OpenSource,IBM和许多其他公司的各种不同的软件包.我还没有找到一种没有某种我无法使用的令人困惑的安装方法,或者与某种我似乎无法使用的第三方组件进行某种集成的方法.

I have been at this for a while, trying all kinds of different packages from openSource, IBM, and many others. I have not yet found one that works without some sort of confusing install method that I can not get to work, or some sort of integration with other third-party pieces that I can not seem to get working.

我只是试图使用Python在Informix Server上执行SQL语句.与mySQL和其他工具没什么不同.使用游标或完整结果转储,真的不在乎.我希望能够静态或动态地形式化查询字符串,然后告诉执行该查询并返回结果(如果有)的任何工具/模块.

I am simply trying to perform SQL statements on a Informix Server using Python. No different than mySQL and other tools. Using cursors or full result dumps, really do not care. I want to be able to formalize a query string statically or dynamically and then tell whatever tools/module to execute said query and return results (if any).

我尝试过:

  • ibm_db 2.0.5.1 (https://pypi.python.org/pypi/ibm_db)
  • IBM Informix Client SDK
  • pymssql
  • unixODBC
  • Looked at but do not want to use Jython (JPython).

我所管理的:

  • 我已经能够安装并获得IBM Informix Client SDK的安装并可以正常工作.我可以连接到我的Informix DB服务器并执行查询.
  • 我可以使用mySQL进行连接和查询.
  • 我已经编写了一个Java程序来使用Java驱动程序执行查询,将其编译,然后将其与bash脚本结合起来以执行查询和电子邮件结果.

我很困惑.在寻求有关下载内容(URL),如何进行安装(技巧和窍门,环境变量,安装位置等)方面的帮助.我想拥有一些不依赖Java或编写Java的东西,等等.我正在寻找一种解决方案,该解决方案将使我能够编写Python以查询,插入,更新和删除Informix数据库和表.我想将以前编写的Java和Bash脚本合并到P​​ython脚本中.

I am just stumped. Looking for assistance on what to download (URLs), how to go about installing it (tips and tricks, environment variables, where to install it, etc..) I want to have something that does not depend on Java or writing Java, etc. I am looking for a solution that may will give me the ability to write Python to query, insert, update, and delete from an Informix database and tables. I want to combine my previously written Java and Bash script into a Python script.

沮丧并寻求任何帮助.

感谢您的收听,如果您不理解我的请求,请提出问题.

Thank you for listening and please ask questions if you do not understand my plea.

推荐答案

Linux上的Informix令人不胜其烦.为了使Informix-connect与CPython3配合使用,我的个人设置是将Informix Client SDK与unixODBC和pyodbc堆叠在一起.有一些问题需要解决,但没有记录在案.几乎所有设置都是完全无用的,但需要使用它来防止Informix驱动程序的某些部分崩溃.请注意,某些选项区分大小写和空格(Description=Informix!= description = Informix).

Informix on Linux is a bag of pain. My personal setup to get Informix-connect to work with CPython3 is stacking the Informix Client SDK with unixODBC and pyodbc. There are some hoops to jump through, none of which are documented. Almost all the setup is completely useless yet required to prevent some parts of the Informix-driver to bail out. Note that some options are case- and space-sensitive (Description=Informix != description = Informix).

  • 安装Informix Client SDK.您不需要包装中附带的所有垃圾,仅需要Informix Connect.我假设您使用默认路径/opt/IBM/informix
  • /opt/IBM/informix/lib/cli/opt/IBM/informix/lib/esql添加到动态链接程序查找路径.在Fedora上,您可以通过将其放入新文件/etc/ld.so.conf.d/informix.conf
  • 中来执行此操作
  • 创建一个新的/etc/odbc.ini并添加以下内容:

  • Install the Informix Client SDK. You don't need all the garbage that comes in the package, just Informix Connect. I assume you use the default path /opt/IBM/informix
  • Add /opt/IBM/informix/lib/cli and /opt/IBM/informix/lib/esql to your dynamic linker lookup paths. On Fedora you can do this by putting them in a new file /etc/ld.so.conf.d/informix.conf
  • Create a new /etc/odbc.ini and add the following:

[ODBC Data Sources] Infdrv1=IBM INFORMIX ODBC DRIVER [Infdrv1] Driver=/opt/IBM/informix/lib/cli/iclit09b.so Description=Informix Database=WHATEVER_YOUR_DB_NAME_IS Servername=WHATEVER_YOUR_SERVER_NAME_IS CLIENT_LOCALE=en_us.8859-1 # MAY BE DIFFERENT DB_LOCALE=en_us.819 # MAY BE DIFFERENT [ODBC] UNICODE=UCS-2

[ODBC Data Sources] Infdrv1=IBM INFORMIX ODBC DRIVER [Infdrv1] Driver=/opt/IBM/informix/lib/cli/iclit09b.so Description=Informix Database=WHATEVER_YOUR_DB_NAME_IS Servername=WHATEVER_YOUR_SERVER_NAME_IS CLIENT_LOCALE=en_us.8859-1 # MAY BE DIFFERENT DB_LOCALE=en_us.819 # MAY BE DIFFERENT [ODBC] UNICODE=UCS-2

创建一个新的/etc/odbcinst.ini并添加以下内容

Create a new /etc/odbcinst.ini and add the following

[IBM INFORMIX ODBC DRIVER] Description=Informix Driver Driver=libifcli.so

[IBM INFORMIX ODBC DRIVER] Description=Informix Driver Driver=libifcli.so

您需要设置环境变量INFORMIXDIRODBCINI.在Fedora上,您可以添加一个新文件/etc/profile.d/informix.sh并添加

You need to set the environment variables INFORMIXDIR and ODBCINI. On Fedora you may add a new file /etc/profile.d/informix.sh and add

export INFORMIXDIR=/opt/IBM/informix export ODBCINI=/etc/odbc.ini

export INFORMIXDIR=/opt/IBM/informix export ODBCINI=/etc/odbc.ini

编辑/opt/IBM/informix/etc/sqlhosts并将您的基本连接信息放在此处.在最简单的情况下,它只有一行可以读取

Edit /opt/IBM/informix/etc/sqlhosts and put your basic connection information there. In the most simple case it has only one line that reads

YOUR_SERVER_NAME\tonsoctcp\tYOUR_DB_NAME\tpdap-np

请注意,pdap-np实际上是端口1526,它也是Informix"Turbo" -Driver tcp端口.查看您的/etc/services

Note that pdap-np is actually port 1526 which is also the Informix "Turbo"-Driver tcp port. See your /etc/services

$HOME中创建一个空的.odbc.ini,例如通过touch $HOME/.odbc.ini.它必须在那里.必须为0个字节.我喜欢这部分.

Create an empty .odbc.ini in your $HOME e.g. by touch $HOME/.odbc.ini. It needs to be there. It needs to be 0 bytes. I love this part.

从您喜欢的存储库中安装unixODBC和pyodbc.

Install unixODBC and pyodbc from your favorite repository.

记住要进行环境更改,例如通过重启.您现在可以像这样连接:

Remember to get your env-changes going, e.g. via reboot. You can now connect like this:

import pyodbc
DRIVER = 'IBM INFORMIX ODBC DRIVER'
SERVER = 'YOUR_SERVER_NAME' 
DATABASE = 'YOUR_DB_NAME'
constr = 'DRIVER={%s};SERVER=%s;DATABASE=%s;UID=%s;PWD=%s' % (DRIVER, SERVER, DATABASE, USER, PASS)
con = pyodbc.connect(constr, autocommit=False)

从那里可以获取光标,执行查询,获取结果等.请注意,在我的脑海中,IBM的ODBC驱动程序中存在许多怪异的错误:

From there on you can get your cursor, execute queries, fetch results and such. Note that there are numerous bugs in quirks in IBM's ODBC-driver, out of my head:

    包含NULL
  • 行可能会导致segfault,因为IBM驱动程序将32位int放置在期望64位int表示该值为空的地方.如果您受到此影响,则需要为所有可能的列类型修补unixODBC来解决此问题.
  • 没有名称的列会导致驱动程序出现段错误(例如SELECT COUNT(*) FROM foobar必须为SELECT COUNT(*) AS c FROM foobar).
  • 确保您的编码实际可以按预期工作.对于IBM而言,UTF8不够企业级,而UCS-2是我唯一要做的事情.
  • Rows that contain NULLs may cause a segfault as the IBM driver puts a 32bit int where a 64bit int is expected to signal the value being null. In case you are affected by this, you need to patch unixODBC for all possible column types to deal with this.
  • Columns without names cause the driver to segfault (e.g. SELECT COUNT(*) FROM foobar needs to be SELECT COUNT(*) AS c FROM foobar).
  • Make sure your encoding actually works as expected. UTF8 is something not enterprise-enough for IBM and UCS-2 is the only thing I got to work.

这篇关于如何在Linux上使用Python和Informix进行通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆