python - 执行查询时 PyODBC Python 3 错误(Ubuntu 14.04)

标签 python python-3.x ubuntu-14.04 pyodbc netezza

我正在尝试使用 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 连接工作正常。 是吗?


网上没有太多关于此的信息。以下是我根据找到的一些博客所做的一些事情。 我的理解正确吗?

  1. 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 上运行,这是否意味着此参数设置正确?

  2. document to configure PyODBC 中提到我应该用 UCS2 版本构建 Python。但是在我的机器上,Python2.7Python3.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文件,设置 UnicodeTranslationOptionutf16 :

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/

相关文章:

python - 如何自动选择和处理/root/facedetect 目录中的每个图像而不是选择特定图像

python - Python 编程 : Controlling Stepper Motor + Raspberry Pi Zero + L293D IC

unix - 在运行apache时找出webroot 2

python - 删除不需要的字符并编辑 Pandas 中的列名

python - 将 bool 值转换为自定义字符串

Python 3 将 ping 设置为变量返回 0

mysql - 使用 python 代码中的变量将值添加到 sql 时显示错误

python - 如何在 AWS ubuntu 上启用端口 5000

docker - 找不到 Hyperledger Docker 端点

python - 将记录器消息存储在字符串中