I've look for solutions to this error but I'm still striking out. I'm using a python 2.7 venv with pyodbc 3.0.6
connection = pyodbc.connect(myconnstring)
cursor = connection.cursor()
cursor.execute("""SELECT varchar_column, date_column as epoch, decimal_col1, decimal_col2
FROM table;""")
rows = cursor.fetchall()
#do some more stuff down here with rows.
Basically it errors out when it's trying to fetch the data from the database in the query specified. I've tried filtering out null results and 0 results, which doesn't help. I've tried casting it to a varchar and using isnull to catch anything that won't have data and nothing seems to be making a difference.
I've tried running the query in the database and it returns fine and if i try to filter for nulls the results are exactly the same so there effectively are no null results. If I filter out 0's there are about 100 less rows but even when I take those out of the script it's still throwing an error which doesn't make sense to me.
Traceback (most recent call last):
File "myodbcscript.py", line 29, in <module>
rows = cursor.fetchall()
File "/opt/python-2.7.3/lib/python2.7/decimal.py", line 548, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/python-2.7.3/lib/python2.7/decimal.py", line 3866, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: u'1-'
As requested in the comments, the DBMS I was trying to connect to was Vertica and the issue wasn't with Vertica itself the queries work fine on there. It was the combination of CentOS 6.4, a python 2.7 venv and Vertica 6.1 driver that resulted in this odd behaviour. As it turns out I was converting my date_column to an epoch and in doing so the result was not being read correctly. It worked fine if it was being returned with only numeric columns but not if I introduced a varchar column.