Problem with psycopg2, bytea, and memoryview

F

Frank Millman

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top