Problem with psycopg2, bytea, and memoryview
frank at chagford.com
Wed Jul 31 11:44:01 CEST 2013
I don't know if this question is more appropriate for the psycopg2 list, but
I thought I would ask here first.
I have some binary data (a gzipped xml object) that I want to store in a
database. For PostgreSQL I use a column with datatype 'bytea', which is
their recommended way of storing binary strings.
I use psycopg2 to access the database. It returns binary data in the form of
a python 'memoryview'.
My problem is that, after a roundtrip to the database and back, the object
no longer compares equal to the original.
>>> memoryview(b'abcdef') == b'abcdef'
>>> cur.execute('create table fmtemp (code int, xml bytea)')
>>> cur.execute('insert into fmtemp values (%s, %s)', (1, b'abcdef'))
>>> cur.execute('select * from fmtemp where code =1')
>>> row = cur.fetchone()
(1, <memory at 0xb725f77c>)
>>> row == b'abcdef'
>>> row.tobytes() == b'abcdef'
Using MS SQL Server and pyodbc, it returns a byte string, not a memoryview,
and it does compare equal with the original.
I can hack my program to use tobytes(), but it would add complication, and
it would be database-specific. I would prefer a cleaner solution.
Does anyone have any suggestions?
Versions - Python: 3.3.2 PostgreSQL: 9.2.4 psycopg2: 2.5
More information about the Python-list