Connecting to Informix with pyodbc on Windows - python

I am attempting to write a process in python that needs to be portable between different database environments. One of the environments that it must be able to connect to is Informix.
I've been searching for how to connect to Informix in Python and have come across both InformixDB and ibm_db{,_sa}, both of which seem overly difficult to use (and I've tried and tried and just can't get them working).
I'm trying (again) to get this working with pyodbc but can't establish a connection to the database from Windows:
set INFORMIXDIR="C:\Program Files\IBM Informix Client SDK"
set CLIENT_LOCALE=en_US.CP1252
set DB_LOCALE=en_US.819
python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import pyodbc
>>>
>>> cnxn = pyodbc.connect(dsn='devdb')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', '[HY000] [Informix][Informix ODBC Driver][Informix]Unspecified System Error = -23101. (-23101) (SQLDriverConnect)')
From what I've found, Error -23101 is caused by the Locale's not matching correctly, however these are the same values used in the ODBC configuration as well as any other Informix-related utility that I have at my disposal.
I am at a loss to figure out how to connect to Informix and can't think of any more search terms to use to try and figure out this issue. How can it be so difficult to use ODBC - almost every other language I know doesn't have a problem with it!
Note: just to be clear, the ODBC connection is configured correctly and works with other ODBC-based applications (I can connect using QTODBC or Perl DBI).
Thanks in advance for any help.
Edit: Heh, I'm not reputable enough to attach an image yet, but I've uploaded it at http://wraeth.id.au/wp-content/uploads/2014/10/odbcad32.png if you want to have a look.
Edit 2:
Updated to ActivePython-3.3.4 with no change
Created symbolic link to IFX ClientSDK dir to remove spaces in path resulting in "The driver did not supply an error"
Confirmed DB_LOCALE is en_US.819 by checking the sysdbslocale table in the database.
Also, confirmed that %INFORMIXDIR% is set to a valid CSDK installation:
> mklink /D informix "C:\Program Files\IBM Informix Client SDK"
> set INFORMIXDIR=C:\informix
> dir %INFORMIXDIR%\gls
Volume in drive C has no label
Volume Serial Number is 808D-98FF
Directory of C:\informix\gls
19/09/2013 04:50 PM <DIR> .
19/09/2013 04:50 PM <DIR> ..
19/09/2013 04:50 PM <DIR> cm3
19/09/2013 04:50 PM <DIR> cv9
19/09/2013 04:50 PM <DIR> dll
19/09/2013 04:50 PM <DIR> etc
19/09/2013 04:50 PM <DIR> lc11
Using the symbolic link as INFORMIXDIR still doesn't allow it to connect:
ActivePython 3.3.4.1 (ActiveState Software Inc.) based on
Python 3.3.4 (default, Feb 25 2014, 15:11:05) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, pyodbc
>>> os.path.exists(
... os.path.join(
... os.environ.get('INFORMIXDIR'),
... 'gls'
... )
... )
True
>>> cnxn = pyodbc.connect(dsn='devdb', uid='user', pwd='password')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
pyodbc.Error: ('HY000', 'The driver did not supply an error!')
Solution
Uninstalling and reinstalling the Client SDK to a path that didn't have spaces (C:\informix) seems to have resolved the issue.

It should be comment, but is too long.
While I cannot reproduce your error I have some ideas.
Reinstall ClientSDK and install it in c:\informix directory setting it as %INFORMIXDIR%. This should change ODBC registry entries about driver:
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\IBM INFORMIX ODBC DRIVER]
"Driver"="C:\\informix\\bin\\iclit09b.dll"
"Setup"="C:\\informix\\bin\\iclit09b.dll"
and about database (devdb is my DSN)
[HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\devdb]
"Driver"="C:\\informix\\bin\\iclit09b.dll"
(those entries are from 32 bit Windows)
I advice it after reading answers and comments to: Informix connection works through Windows, but not through Cygwin
If you have ActiveState Python then you can use odbc module instead of pyodbc. Is is part of win32 package and works only on Windows but maybe it can connect with your database. You can open database with:
import odbc
cnxn = odbc.odbc('devdb/user/password')
If some ODBC software works then you can enable tracing ODBC and compare traces. It would be useful especially if you can connect from odbc module but not from pyodbc.

Related

Can't load hidapi with Python library "hid" on Windows

sorry if the question is not well asked)
I'm trying to use the Python library hid, which rely on the hidapi library. hid seems to not be able to load hidapi, as it's telling me here:
$ python
Python 3.10.1 (tags/v3.10.1:2cd268a, Dec 6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import hid
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "[...]\Python310\site-packages\hid\__init__.py", line 30, in <module>
raise ImportError(error)
ImportError: Unable to load any of the following libraries:libhidapi-hidraw.so libhidapi-hidraw.so.0 libhidapi-libusb.so libhidapi-libusb.so.0 libhidapi-iohidmanager.so libhidapi-iohidmanager.so.0 libhidapi.dylib hidapi.dll libhidapi-0.dll
The problem is that I have downloaded one of those libraries from hidapi's release page (the Windows x64 version), but it doesn't seem to matter where I put it, it doesn't work. It is in my PATH:
$ echo $PATH
[...]:/c/Program Files/hidapi:[...]
$ ls -lh "/c/Program Files/hidapi"
total 3.0M
-rwxr-xr-x 1 [...] 197121 98K Jan 28 14:38 hidapi.dll*
-rw-r--r-- 1 [...] 197121 5.7K Jan 28 14:38 hidapi.lib
-rw-r--r-- 1 [...] 197121 2.9M Jan 28 14:38 hidapi.pdb
Even if I put it in the folder where I'm running Python from, or in System32, the same error happens again. This post didn't worked either
EDIT: I didn't solve the core problem, but the ctypes library that is trying to load the library isn't skiping it because it can't find it but because it isn't a valid Win32 app ([WinError 193] %1 n’est pas une application Win32 valide is the error, in french though). Maybe it is be cause it's not registered, but I'm not sucessful at trying to register it.
I've found a workaround: manually load the hidapi library before loading the hid library
import ctypes
ctypes.CDLL('[my path to the DLL]\\hidapi.dll')
import hid
I put hidapi.dll and hidapi.lib in C:\Users\<Username>\AppData\Local\Programs\Python\Python310 and it worked ! :-)

Python sqlite3 library can't use URIs, even though sqlite3 version should be able to

I have the most recent sqlite3 installed on my machine:
$ sqlite3 --version
3.26.0 2018-12-01 12:34:55 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9
And, in python, the sqlite3 module is using this version of sqlite3:
$ python
Python 3.4.9 (default, Jan 5 2019, 18:35:56)
[GCC 5.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3 as sq
>>> sq.sqlite_version_info
(3, 26, 0)
>>> sq.version_info
(2, 6, 0)
However, I cannot open a database file using URIs, even though that feature has been present in sqlite since version 3.7:
>>> import sqlite3 as sq
>>> c = sq.connect('file://test', uri=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.NotSupportedError: URIs not supported
What is going on here? What have I done wrong?
OK, I figured out what was going on by reading the source code. When pyenv compiled my version of Python, the _sqlite module was compiled against the ridiculously old CentOS version of the "sqlite3.h" file. Because of this, the Python module didn't have the SQLITE_OPEN_URI macro defined, which causes it give a hard-coded "URIs not supported" Python exception.
To get around this, I had to set the following environment variable:
# This is to direct pyenv to the linuxbrew include and library directories, when building versions of Python
export PYTHON_CONFIGURE_OPTS="LD_RUN_PATH=/home/linuxbrew/.linuxbrew/lib/ LDFLAGS=-L/home/linuxbrew/.linuxbrew/lib/ CPPFLAGS=-I/home/linuxbrew/.linuxbrew/include/"

iPython: DLL load failed: The specified module could not be found; plain Python fine

I keep getting this (well known) error in iPython. Yet, the same import works fine in plain Python. (Python 3.3.5, see details below)
iPython:
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 2.0.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import test1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-7-ddb30f03c287> in <module>()
----> 1 import test1
ImportError: DLL load failed: The specified module could not be found.
Python (not only it loads fine, it also works):
$ python
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar 9 2014, 10:37:12) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import test1
>>>
Now, Dependency Walker on test1.pyd shows this
[ ? ] LIBGCC_S_DW2-1.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] LIBSTDC++-6.DLL Error opening file. The system cannot find the file specified (2).
[ ? ] PYTHON33.DLL Error opening file. The system cannot find the file spec
I even overwrote sys.path in iPython with the one from plain Python. The file test1.pyd is in C:\Test.
['c:\\Test',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\python33.zip',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\DLLs',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\FontTools',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\win32\\lib',
'c:\\WinPython-32bit-3.3.5.0\\python-3.3.5\\lib\\site-packages\\Pythonwin']
Why would the import work in plain Python but not in iPython?
I have encountered the same problem. After hours looking and thinking I found out the cause. The difference is environment variables between interpreters (plain python and ipython or pycharm etc.). I think your can use %env in ipython to check the environment variables. In plain python, use (works in python 3.7):
import os
os.environ
Then if there are differences, maybe you should set the right one before your run.
Actually there are multiple ways to set envs. For example
os.environ['key']='value' #Both key and value are strings
or
os.putenv('key', 'value')
Here key is the name of the environment variable, and value is the value you want to set it to.
Hope this helps you.~~~///(^v^)\\~~~

python import cx_Oracle error on cygwin

I tried to install cx_Oracle from pypi source since there is no available port for it in cygwin. I did make some changes as suggested in http://permalink.gmane.org/gmane.comp.python.db.cx-oracle/2492 and modified my setup.py. However, I still get the following error :-
$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
/usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-py2.7-cygwin-1.7.24-i686.egg/cx_Oracle.py:3: UserWa
rning: Module cx_Oracle was already imported from /usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-p
y2.7-cygwin-1.7.24-i686.egg/cx_Oracle.pyc, but /home/zerog/cx_Oracle-5.1.3 is being added to sys.pat
h
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.cygwin-1.7.24-i686/egg/cx_Oracle.py", line 7, in <module>
File "build/bdist.cygwin-1.7.24-i686/egg/cx_Oracle.py", line 6, in __bootstrap__
ImportError: Exec format error
>>>
If someone can please help me fix this ?
TIA.
Fixed this by specifying the path to instantclient as below :
$ export PATH=$PATH:/cygdrive/d/Tools/instantclient_11_2
(Other, possibly important stuff) :
$ echo $LD_LIBRARY_PATH
/cygdrive/d/Tools/instantclient_11_2
$ echo $ORACLE_HOME
/cygdrive/d/Tools/instantclient_11_2
Now, I get :-
$ python
Python 2.7.3 (default, Dec 18 2012, 13:50:09)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>>
It's hard to pin down from the error message alone, but I am guessing that you have two different copies of cx_Oracle in your sys.path. The error message is complaining that a different version of the same module had already been import-ed.
Presumably the pristine upstream version is installed system-wide in /usr/lib/python2.7/site-packages/cx_Oracle-5.1.3-py2.7-cygwin-1.7.24-i686.egg, and your modified version in /home/zerog/cx_Oracle-5.1.3.
Does it work if you pare down sys.path so that only the original, or only your modified version, is included?
(You might want to use virtualenv if you need to switch back and forth between two versions frequently.)
I ran into this error "Exec format error."
For me, this was likely caused by a mismatch between cygwin being installed as 64 bit, but the instant client being installed as 32 bit. Double check that everything (oracle, cygwin) is either 32 bit or 64 bit.
What fixed my issue:
Since my cygwin is 64 bit (see uname -a, and look for x86_64), I downloaded the 64 bit instant client from oracle's website, and unzipped
I set the env vars in .profile, to point where it was unzipped:
export ORACLE_HOME=/cygdrive/c/oracle/instantclient_x64_11_2
export LD_LIBRARY_PATH=$ORACLE_HOME
export DYLD_LIBRARY_PATH=$ORACLE_HOME
export TNS_ADMIN='//optional/path/to/your/oracle/tns/files/'
source ~/.profile
To test, you should now be able to run this python command with no error:
import cx_Oracle
To verify the path is correct, if you run ls, you should see something like
ls $ORACLE_HOME
adrci.exe genezi.exe oci.sym ociw32.dll ojdbc6.jar
oraocci11.dll oraociei11.sym uidrvci.exe vc9
adrci.sym genezi.sym ocijdbc11.dll ociw32.sym orannzsbb11.dll
oraocci11.sym orasql11.dll uidrvci.sym xstreams.jar
BASIC_README oci.dll ocijdbc11.sym ojdbc5.jar orannzsbb11.sym
oraociei11.dll orasql11.sym vc8

Running SL4A facade APIs from host python shell

Objective:
Trying to run SL4A facade APIs from python shell on the host system (windows 7 PC)
My environment:
1. On my windows 7 PC, i have python 2.6.2
2. Android sdk tools rev 21, platform tools rev 16
3. API level 17 supported for JB 4.2
4. I have 2 devices ( one running android 2.3.3 and another android 4.2.2) both running Python for android and SL4A
I'm trying these commands as specified at http://code.google.com/p/android-scripting/wiki/RemoteControl
Here are the commands which i'm trying on the python shell:
Python 2.6.2 (r262:71605, Apr 14 2009, 22:40:02) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid=android.Android
>>> droid.makeToast("Hello")
Traceback (most recent call last):
File "", line 1, in
AttributeError: type object 'Android' has no attribute 'makeToast'
Before this i'm performing the port forwarding and starting a private server as shown below
$ adb forward tcp:9999 tcp:4321
$ set AP_PORT=9999
Also set the server on the target listening on port 9999 ( through SL4A->preferences->serverport.
Please help to understand where i'm doing mistake which gives the above error while trying droid.makeToast("Hello") ?
Try
import android
droid=android.Android()
droid.makeToast("Hello")
(you are missing parentheses after android.Android)
Hope you solved the problem. This is what I found, so maybe for somebody else it will be useful.
You need to run all your commands inside the DOS shell...otherwise you will get that error[11001].
If you still want to run you commands outside the DOS shell - add a new environment variable as described here:
http://www.smartphonedaq.com/installation.page

Categories