简体   繁体   中英

PyODBC Python 3 error while executing query (Ubuntu 14.04)

I am trying to configure ODBC on Ubuntu 14.04 with Python 3.4.3. I am able to successfully make a connection, but an getting this error on execution:

>>> cursor.execute("SELECT * FROM xxx.yyy.zzz LIMIT 100;")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
pyodbc.ProgrammingError: ('42000', '[42000] ERROR:  \'S\'\nerror    ^ found "S" (at char 1) expecting a keyword (27) (SQLExecDirectW)')

The same code to execute the query is working fine on Python 2.7. I am also able to make a connection from the UNIX shell using strace isql , which makes me think that my ODBC connection is working fine. Is it?


There is not much information available online regarding this. Below are the few things I have done based on a few blogs I found. Is my understanding correct?

  1. As is mentioned in PyODBC ticket :

    In my case the ODBC driver (NetezzaSQL) was set to use UTF8. I first though the issue was either a UC2 or UC4 build of Python. Both made no difference. Switching the driver to UTF16 (UnicodeTranslationOption=utf16) fixed the issue for me.

    I have UnicodeTranslationOption=utf16 set in all my obdc*.ini files. Since my code is running on Python 2.7, does that mean this parameter is set correctly?

  2. It is mentioned in document to configure PyODBC that I should build Python with UCS2 version. But on my machine, both Python2.7 and Python3.4 says they have UCS4 version, and since code is running on Python 2.x, that means UCS4 is not the issue here.

    I checked UCF version using below commands:

     user@host:~$ python -c "import sys;print(sys.maxunicode<66000 and'UCS2'or'UCS4')" UCS4 user@host:~$ python3 -c "import sys;print(sys.maxunicode<66000 and'UCS2'or'UCS4')" UCS4 

In your /etc/odbcinst.ini file, set UnicodeTranslationOption to utf16 :

UnicodeTranslationOption = utf16

Then copy /etc/odbcinst.ini to /home/<user>/.odbcinst.ini

Another trick would be to copy the /etc/odbc.ini to /home/<user>/.odbc.ini so that you don't need to export the following variables:

  • LD_LIBRARY_PATH
  • ODBC_INI
  • NZ_ODBC_INI_PATH

You have to force the pyodbc connection to use UTF-8. (and on that note, leave the odbcinst.ini file with the "UnicodeTranslationOption=utf8" setting)

After creating your connection, do the following before using it:

connection.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WCHAR, encoding='utf-8')
connection.setdecoding(pyodbc.SQL_WMETADATA, encoding='utf-8')
connection.setencoding(encoding='utf-8')

I had this same issue, and this was the only thing that worked for me. More info can be found in the pyodbc documentation on github: https://github.com/mkleehammer/pyodbc/wiki/Connecting-to-Netezza

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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