Psql parsing error while moving cursor .fetchall ()

I have python code that queries psql and returns a batch of results with cursor.fetchall(). It throws an exception and refuses the process if the casting failed due to bad data in the database. I get this exception:

File "/usr/local/lib/python2.7/site-packages/psycopg2cffi/_impl/cursor.py", line 377, in fetchall return [self._build_row() for _ in xrange(size)] File "/usr/local/lib/python2.7/site-packages/psycopg2cffi/_impl/cursor.py", line 891, in _build_row self._casts[i], val, length, self) File "/usr/local/lib/python2.7/site-packages/psycopg2cffi/_impl/typecasts.py", line 71, in typecast return caster.cast(value, cursor, length) File "/usr/local/lib/python2.7/site-packages/psycopg2cffi/_impl/typecasts.py", line 39, in cast return self.caster(value, length, cursor) File "/usr/local/lib/python2.7/site-packages/psycopg2cffi/_impl/typecasts.py", line 311, in parse_date raise DataError("bad datetime: '%s'" % bytes_to_ascii(value)) DataError: bad datetime: '32014-03-03'

Is there a way to tell the caster to ignore this error and parse it as a string instead of failing the whole batch?

+4
source share
2 answers

You can hack the parser psycopg2cffito return DATE objects as strings:

code, DATE, DATE .

import psycopg2cffi

psycopg2cffi._impl.typecasts._default_type('DATE', [1082],
                                  psycopg2cffi._impl.typecasts.parse_string)

, .

+2

psql-

. date_column_name:: to_char table_name.

0

Source: https://habr.com/ru/post/1626143/


All Articles