1. Background
I recently needed to use Python to connect to an IBM Informix database.
System environment.
- Cent7 OS
- Python 3.7.2
- Informix database
I tried to connect to Informix using DBeaver as a client and confirmed that there is no problem connecting to the database locally. The following connection information is assumed
- Host: 127.0.0.1
- Port: 50000
- Username: chancel
- Password: chancel
- Database: my-test
I used multiple connection methods on the Python side and encountered various strange problems, including timeout errors, non-existent databases, driver errors, and so on.
Considering that DBeaver is using JDBC to connect, there is no problem to connect, finally I use Python+JDBC to connect to the database successfully.
If DBeaver has no connection problems and other libraries have more or less the same problems as me, then you can give priority to JayDeBeApi
connection method.
2. Programs
2.1. JayDeBeApi (recommended)
Install jaydebeapi.
|
|
The jdbc driver needs to be downloaded manually
Download and extract to the specified directory, I store it in
|
|
The Python connection code is as follows.
The connection is normal and the data can be queried normally.
2.2. SQLAlchemy
Install SQLAlchemy.
|
|
Be careful to install sqlalchemy-informix
, otherwise the following error will occur.
Write the code for the connection.
The error after execution is as follows, showing that the database connection does not exist, and this error still occurs after several attempts and cannot be resolved.
2.3. ifpyx
The implementation can be found in the Python repository: OpenInformix/IfxPy - github.com.
According to the documentation not only need ODBC Diver but also need to deploy CSDK, the deployment cost is higher.
Reference code.
After execution it still gives an error and shows a missing so library file. I have installed a higher version of IBM Clent SDK and there is no SO file corresponding to this library.
After reviewing the documentation, I feel that the problem may lie in the CSDK installation.
2.4. ibm_db
The implementation can be found in the Python repository: ibmdb/python-ibmdb - github.com.
Install ibm_db.
|
|
Verify that the connection is possible with the code.
Execution error, after several attempts, it still indicates that the database name does not exist, the reason is unknown.
2.5. pydoc
Implementation reference Python repository: mkleehammer/pyodbc - github.com.
According to the official documentation, we need to use the ODBC driver to use it, you can refer to the documentation for different installation systems.
For Ubuntu 1804.
For CentOS7.
Check the installation.
|
|
Test the connection with code.
Prompt “CData ODBC Driver for Informix file not found”.
This does not happen with Ubuntu 1804, but only with CentOS 7. It is verified that CentOS7 needs to manually create odbcinst.ini
and odbc.ini
files under /etc.
No more exceptions, but unfortunately, it keeps showing connection timeout, guessing that it is related to the database version of Informix.