简体   繁体   English

在Python / Pyodbc中查询Teradata的问题

[英]Issue with querying Teradata in Python/Pyodbc

I'm trying to query a Teradata database in Python with PyODBC. 我正在尝试使用PyODBC在Python中查询Teradata数据库。 The connection to database is established alright; 与数据库的连接建立得很好; however, when I try to fetch result, I ran into this error "Invalid literal for Decimal: u''". 但是,当我尝试获取结果时,我遇到了这个错误“无效的文字为十进制:你''”。 Help please. 请帮忙。

I am on RHEL6, with Python 2.7.3 我在RHEL6上,使用Python 2.7.3

Here is the code and result: 这是代码和结果:

import pyodbc

sql = "select * from table"

pyodbc.pooling = False
cnx = pyodbc.connect("DRIVER={Teradata};DBCNAME=host;DATABASE=database;   AUTHENTICATION=LDAP;UID=user;PWD=password", autocommit=True, ANSI=True)
cursor = cnx.cursor()
rows = cursor.execute(sql).fetchone()

InvalidOperation                          Traceback (most recent call last)
<ipython-input-25-f2a0c81ca0e4> in <module>()
----> 1 test.fetchone()

/usr/local/lib/python2.7/decimal.pyc in __new__(cls, value, context)
    546                     context = getcontext()
    547                 return context._raise_error(ConversionSyntax,
--> 548                                 "Invalid literal for Decimal: %r" % value)
    549 
    550             if m.group('sign') == "-":

/usr/local/lib/python2.7/decimal.pyc in _raise_error(self, condition, explanation, *args)
   3864         # Errors should only be risked on copies of the context
   3865         # self._ignored_flags = []
-> 3866         raise error(explanation)
   3867 
   3868     def _ignore_all_flags(self):

InvalidOperation: Invalid literal for Decimal: u''

我有这个错误,我发现原因是pyodbc是64位,而我的teradata驱动程序是32.问题是用unixodbc构建一个新驱动程序后解决的。

Forcing the right locale in setup.py (before compiling) did the trick for me ,example: 在setup.py中强制使用正确的语言环境(在编译之前)为我做了诀窍,例如:

import locale
locale.setlocale(locale.LC_ALL, 'es_ES.utf8')

I found that pyodbc out of the box might not work as per http://code.google.com/p/pyodbc/issues/detail?can=1&q=teradata&id=146 我发现开箱即用的pyodbc可能无法正常工作http://code.google.com/p/pyodbc/issues/detail?can=1&q=teradata&id=146

I verified the fix in that thread works. 我验证了该线程的修复工作原理。 Check out https://github.com/mkleehammer/pyodbc , add extra commands to setup.py, recompile, and it appears to work. 查看https://github.com/mkleehammer/pyodbc ,向setup.py添加额外的命令,重新编译,它似乎工作。

If your null character is set to '?' 如果您的空字符设置为'?' (which I believe is the default) or anything else strange this could be a problem with returning, especially if you are working with the data once it hits python. (我认为这是默认值)或其他任何奇怪的事情,这可能是返回的问题,特别是如果你在使用python时处理数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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