我正在尝试使用 Python 3.4.3 在 Ubuntu 14.04 上配置 ODBC。我能够成功建立连接,但在执行时出现此错误:
>>> 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)')
执行查询的相同代码在 Python 2.7 上运行良好。我还能够使用 strace isql
从 UNIX shell 建立连接,这让我认为我的 ODBC 连接工作正常。 是吗?
网上没有太多关于此的信息。以下是我根据找到的一些博客所做的一些事情。 我的理解正确吗?
如 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.
我在所有
obdc*.ini
文件中设置了UnicodeTranslationOption=utf16
。 由于我的代码在 Python 2.7 上运行,这是否意味着此参数设置正确?在 document to configure PyODBC 中提到我应该用
UCS2
版本构建 Python。但是在我的机器上,Python2.7
和Python3.4
都说它们有UCS4
版本,并且因为代码在 Python 2.x 上运行,这意味着UCS4
不是这里的问题。我使用以下命令检查了
UCF
版本: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
最佳答案
在你的/etc/odbcinst.ini
文件,设置 UnicodeTranslationOption
至 utf16
:
UnicodeTranslationOption = utf16
然后复制/etc/odbcinst.ini
至 /home/<user>/.odbcinst.ini
另一个技巧是复制 /etc/odbc.ini
至 /home/<user>/.odbc.ini
这样您就不需要导出以下变量:
-
LD_LIBRARY_PATH
-
ODBC_INI
-
NZ_ODBC_INI_PATH
关于python - 执行查询时 PyODBC Python 3 错误(Ubuntu 14.04),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39835471/