Problem with psycopg2, bytea, and memoryview
Frank Millman
frank at chagford.com
Wed Jul 31 05:44:01 EDT 2013
Hi all
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'
True
>>> 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()
>>> row
(1, <memory at 0xb725f77c>)
>>> row[1] == b'abcdef'
False
>>> row[1].tobytes() == b'abcdef'
True
>>>
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
Frank Millman
More information about the Python-list
mailing list